Reputation: 62
I have a C++ aplication with one form (Form1.h) and a plugin.ccp file that is the actual application .The program is a plugin for Mach3 cnc controler which comunicates with the cnc machine by USB.
I want a global variable that can be used in Form1.h and in plugin.ccp. A tried with a solution i found on this site.
Form1.h :
extern BOOL B1;
Form1.ccp
#include "Form1.h"
BOOL B1 = TRUE ;
plugin.ccp
#include "Form1.h"
And it compiles without errors . But when a type something like this
Form1.h
B1 = FALSE;
// or
SomeOtherVar = B1;
It gives me
Error 1 error LNK2020: unresolved token (0A00003B) "int mach_plugin::B1" (?B1@mach_plugin@@3HA) E:\mach_vmotion\Plugin.obj mach_vmotion Error 2 error LNK2020: unresolved token (0A00000E) "int mach_plugin::B1" (?B1@mach_plugin@@3HA) E:\mach_vmotion\Form1.obj mach_vmotion Error 3 error LNK2001: unresolved external symbol "int mach_plugin::B1" (?B1@mach_plugin@@3HA) E:\mach_vmotion\Form1.obj mach_vmotion Error 4 error LNK2001: unresolved external symbol "int mach_plugin::B1" (?B1@mach_plugin@@3HA) E:\mach_vmotion\Plugin.obj mach_vmotion Error 5 error LNK1120: 3 unresolved externals E:\mach_vmotion\Debug\mach_vmotion.dll mach_vmotion
Upvotes: 1
Views: 629
Reputation: 347
inside Form1.cpp. Try
#include "Form1.h"
namespace mach_plugin{
BOOL B1 = TRUE ;
}
You need to post more codes so that we may see it clearly.
Upvotes: 0
Reputation: 2611
In general, don't try to assign to variables in header files. Whether you put these assignments before or after the extern BOOL B1
(or deleted that line entirely), it won't work correctly. Instead, assign B1
and SomeOtherVar
where they're defined (either via initialization or inside a function).
Upvotes: 0
Reputation: 17163
The error message indicates you have something like :
namespace mach_plugin {
#include "form1.h"
}
while your B1 is defined at global scope. Make up your mind where it belongs and make the declaration in header match -- and avod including files inside any blocks, namespace
, extern "C"
, whatever.
Upvotes: 2