c++ - Undefined reference error when using a simple class inside my function -
i getting nuts error thought of more experienced developers can me in regard. trying compile sample project uses c++ library (named poco). project linked compiled poco libraries.
below (most simplified) code:
#include "poco/uuid.h" class x { void func1() { new poco::uuid(); //a } }; void func1() { new poco::uuid(); //b }
now when above code compiled, line 'a' has no error line 'b' linker says: undefined reference `poco::uuid::uuid()'
what reason? when instantiate class external lib in class method no error occurs same code in function produces linker error? (when comment line b, no error occurs , linker output files generated)
my configuration: win7/g++/codelite/mingw-4.7.1
*update 2:*thanks. problem resolved , issue had compiled library using msvc compiler while application being compiled using g++ (both under windows platform). re-compiled library using g++ , works fine now.
update 1: here ide's output when build project:
c:\windows\system32\cmd.exe /c "mingw32-make.exe -j 4 -e -f "dll1.mk" all" ----------building project:[ dll1 - debug ]---------- g++ -shared -fpic -o ./debug/dll1.so @"dll1.txt" -l. -lc:/poco/lib -lpocofoundationd ./debug/pluginlibrary.o: in function `z5func1v': c:/users/pars/documents/codelite/workspace1/dll1/pluginlibrary.cpp:12: undefined reference `poco::uuid::uuid()' collect2.exe: error: ld returned 1 exit status mingw32-make.exe: *** [debug/dll1.so] error 1 dll1.mk:77: recipe target `debug/dll1.so' failed 1 errors, 0 warnings
your member function x::func1()
never odr-used in compilation unit (source file). compilers generate compiled code member function defined inside class definition if member function odr-used within compilation unit being compiled. suppose other source file use x::func1()
. if compile other source file, compiler produce object code x::func1()
in object file corresponds other source file.
the compiler can away bypassing process of generating compiled code x::func1()
here because class definition has same across compilation units. if compile other source file has different definition of class x have violated 1 definition rule. undefined behavior , no diagnosis required.
if no source file uses x::func1()
have dead code never happens compiled. code has error it's never detected.
the compiler cannot away bypassing generating compiled code free function func1()
. function has external linkage; there's no way compiler can tell if might used somewhere else. compiler must generate compiled code free function.
here's minimum working example:
class missing { public: missing(); int value; }; class usesmissing { public: int use_missing () { missing missing; return missing.value; } int dont_use_missing () { return 0; } }; #ifdef define_use_missing int use_missing () { missing missing; return missing.value; } #endif int main () { usesmissing test; #ifdef use_missing return test.use_missing(); #else return test.dont_use_missing(); #endif }
compile neither define_use_missing
or use_missing
defined , compiles , links fine g++ , clang++. define either 1 of flags , file fails in link step because of undefined reference missing::missing()
.
Comments
Post a Comment