C ++ undefined reference to

Hi

I have some questions about .h and .cpp files in C ++ / linux (ubuntu).

  • You can compile the .h file with g ++, or simply compile the .cpp file containing the .h file.

  • From the .h file and its .cpp file (.cpp, where I include some code in the methods defined in the .h file). I am creating a .so file with the command:

    g++-fPIC -shared my_code.cpp -o my_code.so` 

    In test.cpp I include the .h file and using dlopen, I create a handler on the .so file. Why do I have the following error:

     undefined reference to bool `Class::method(std::string)` `collect2: ld returned 1 exit status 
  • If I say the virtual method bool ... in the .h file, I am not compiling test.cpp. Can someone explain what I'm doing wrong? The fact is that I have a template. With templates, I cannot use virtual..so..i I have this error undefined, and I do not know how to resolve it. thanks

EDIT:

When I compile my_code.cpp , I have errors:

 /usr/bin/ld: .usr/lib/debug/usr/lib/crt1.o relocation 0 has invalid symbol index 12 (same with index 13,2,14...22 ). 

But when I create the .so file, there is no error. I use:

 g++ test.coo -ldl -o test 

to compile test.cpp.

+4
source share
2 answers
  • As a rule, there is no need to compile the .h file, it just generates a huge file with the extension .gch . I suppose.

  • The error you get is the link time. When you create a .so file, you are not actually linking the code. Thus, it is assumed that all undefined characters are present in some place. When you link it, the linker will find for these characters. Thus, you must compile / link the entire .cpp file. The error will disappear.

In addition, for templates , the code definition should always be visible . Therefore, wherever you write a template definition of a function / variable, include this file everywhere.

Edit : You may have a virtual method with templates; but you cannot have virtual template methods .

 template<typename T> class A { virtual void foo(int); // ok }; class A { template<typename T> virtual void foo(T); // illegal }; 
0
source

Ad 1: You can compile the .h file (you can explicitly override the language detection), but you do not want to do this. The .h file is intended to be included and will not compile anything useful to it.

Announcement 2: you need to link to the created library by passing -lmy_code (but note that for this you need to create it as libmy_code.so ) along with the corresponding -L flag (the directory where you put libmy_code.so ) in the linker. Like this:

 g++ test.cpp -L. -lmy_code -ldl -o test 

But you must also change the first command to:

 g++ -fPIC -shared my_code.cpp -o libmy_code.so ^^^ libraries *must* have `lib` prefix on unix systems. 

and this assumes that they both run in the same directory - if not, you must configure the -L option to point to the directory where libmy_code.so is located. You also need to place libmy_code.so somewhere where the dynamic linker can find it. Either set it, or set the environment variable LD_LIBRARY_PATH in the appropriate directory. Alternatively, you can compile with

 g++ test.cpp my_code.so -ldl -o test 

This does not force the lib prefix, and it creates the "rpath" entry in binary format, so it will find the library in its original location.

All of this assumes that you want to use it as a regular library, in which case you do not want to use dlopen . dlopen designed to open libraries as plug-ins at runtime, and can only be accessed by binding pointers to characters using dlsym() , but if you want to access the library in normal mode, you need to link it so that the linker could allow characters.

If instead you wanted to use dlopen , you should not include my_code.h in test.cpp and should not use everything that it defines, except for receiving characters with dlsym . And since it is C ++, this, in turn, requires an understanding of the character conversion scheme, because dlsym will not do this for you.

+2
source

All Articles