Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: clros on November 28, 2010, 08:07:01 pm

Title: Make a program that use a .so
Post by: clros on November 28, 2010, 08:07:01 pm
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)
Title: Re: Make a program that use a .so
Post by: Jenna on November 28, 2010, 08:53:56 pm
Maybe because you forgot to link against the library ?
Title: Re: Make a program that use a .so
Post by: clros on November 28, 2010, 09:37:26 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?
Title: Re: Make a program that use a .so
Post by: Jenna on November 28, 2010, 09:45:03 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 .
Title: Re: Make a program that use a .so
Post by: clros on November 29, 2010, 07:55:40 am
I get the same error  :(
Title: Re: Make a program that use a .so
Post by: 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
Title: Re: Make a program that use a .so
Post by: clros on November 29, 2010, 05:39:39 pm
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:
Code
#include <iostream>
//prototype
int SampleFunction1();

int main()
{
    SampleFunction1();
    std::cout <<SampleFunction1()<< endl;
    return 0;
}
 
Title: Re: Make a program that use a .so
Post by: Jenna 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++.
Title: Re: Make a program that use a .so
Post by: clros on November 29, 2010, 09:41:08 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!!  :)