Author Topic: Make a program that use a .so  (Read 9587 times)

Offline clros

  • Single posting newcomer
  • *
  • Posts: 6
Make a program that use a .so
« 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)
« Last Edit: November 28, 2010, 08:41:51 pm by clros »

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Make a program that use a .so
« Reply #1 on: November 28, 2010, 08:53:56 pm »
Maybe because you forgot to link against the library ?

Offline clros

  • Single posting newcomer
  • *
  • Posts: 6
Re: Make a program that use a .so
« Reply #2 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?

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Make a program that use a .so
« Reply #3 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 .

Offline clros

  • Single posting newcomer
  • *
  • Posts: 6
Re: Make a program that use a .so
« Reply #4 on: November 29, 2010, 07:55:40 am »
I get the same error  :(

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline clros

  • Single posting newcomer
  • *
  • Posts: 6
Re: Make a program that use a .so
« Reply #6 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;
}
 

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Make a program that use a .so
« Reply #7 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++.

Offline clros

  • Single posting newcomer
  • *
  • Posts: 6
Re: Make a program that use a .so
« Reply #8 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!!  :)