android - Undefined Extern Reference in Eclipse -
i'm trying compile simple c++ code in eclipse compiles fine in visual studio.
i have following files (renamed , simplified clarity). main.cpp has other boilerplate code in main function , forth, i've omitted.
foo.h
#ifndef foo_h #define foo_h extern int foo; #endif
foo.cpp
int foo = 0;
main.cpp
#include "foo.h" void bar() { foo = 1; }
when try compile in eclipse, following error (omitted long paths)
main.o: in function bar():jni/main.cpp:25: error: undefined reference 'foo' collect2: ld returned 1 exit status
if hover mouse on foo in main.cpp, shows tooltip it, , if right click on , select open declaration, takes me foo.cpp , highlights foo declaration. finding it, failing compile.
if move declaration main.cpp, below, compiles fine.
main.cpp
#include "foo.h" int foo = 0; void bar() { foo = 1; }
so reason, eclipse not seeing declaration when placed in different cpp file 1 referencing it. why that, , how fix it? unfortunately, moving declaration main.cpp not possible extern needs accessed across number of cpp files.
this error means content of foo.cpp not being linked executable (and possibly not compiled).
Comments
Post a Comment