Hi!
I'm making a program that would use a .so library (I previously created this library again with the code:: blocks and it contains some simple functions es. SampleFunction1() )
When I compile my program, the linker reports the error:
undefined reference to SampleFunction1 ()
Why?
(I am using code::blocks on Ubuntu/GCC)
Maybe because you forgot to link against the library ?
Quote from: jens on November 28, 2010, 08:53:56 PM
Maybe because you forgot to link against the library ?
In "build options -> Linker settings", I entered the library name and its path (actually the .so lib is no in the /lib directory).
This is correct to link a .so library?
Quote from: clros on November 28, 2010, 09:37:26 PM
Quote from: jens on November 28, 2010, 08:53:56 PM
Maybe because you forgot to link against the library ?
In "build options -> Linker settings", I entered the library name and its path (actually the .so lib is no in the /lib directory).
This is correct to link a .so library?
Add the path to "Build options -> Search directories -> Linker" and the library itself to "Build options -> Linker settings -> Link libraries".
Try the libraris name without leading lib and without the .so .
I get the same error :(
Then read this: http://wiki.codeblocks.org/index.php?title=FAQ#Q:_How_do_I_troubleshoot_an_compiler_problem.3F
Quote from: oBFusCATed on November 29, 2010, 09:20:12 AM
Then read this: http://wiki.codeblocks.org/index.php?title=FAQ#Q:_How_do_I_troubleshoot_an_compiler_problem.3F
My command line is:
g++ -L/home/claudio/Scrivania/ProvaShared/bin/Release -o bin/Release/TestShared obj/Release/main.o -s -lProvaShared
obj/Release/main.o: In function `main'
My .so is called "libProvaShared.so"
This invocation sems to be right.
But I have this error:
main.cpp:(.text+0x3c): undefined reference to `SampleFunction1()'
main.cpp:(.text+0x41): undefined reference to `SampleFunction1()'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
2 errors, 0 warn
This is my simple program:
#include <iostream>
//prototype
int SampleFunction1();
int main()
{
SampleFunction1();
std::cout <<SampleFunction1()<< endl;
return 0;
}
How did you declare your function ?
If you use the sample shared library created by the wizard as it is (with extern "C" around the functions implementation), you also have to add it (extern "C") to the declaration in the header file (or wherever you declared it), or leave the extern "C" away (in both places), if you use it as pure c++.
Quote from: jens on November 29, 2010, 06:58:31 PM
How did you declare your function ?
If you use the sample shared library created by the wizard as it is (with extern "C" around the functions implementation), you also have to add it (extern "C") to the declaration in the header file (or wherever you declared it), or leave the extern "C" away (in both places), if you use it as pure c++.
It now work!!
Thanks!! :)