16

i am trying to get a library to work in my c++ project and there is no clear instructions of how to do this for people who are not used to c++

the following link is the closest i have come

it states the following

-L/path/to/my/library/folder -ldllname

also the following thread states the following

gcc yourfile.cpp -lblah

now from what i can see the command is -l + filename, for example my filename is directory/libtest.so it would be -ldirectory/libtest.so, is this correct, could someone clarify

i am currently using the following command to compile my maincpp.cpp file, would like to however include a .so file called for example ./directory/libtest.so

g++ -fPIC -o libgetmacip.so -shared -I $JAVA_HOME/include -I $JAVA_HOME/include/linux maincpp.cpp cpptoinclude.cpp
Community
  • 1
  • 1
basickarl
  • 37,187
  • 64
  • 214
  • 335
  • You have to keep in mind that giving a dynamic library to a binary means just to state that this binary shall at execution time be linked to *a library of that name* (not *that* library). At execution time, such a library must be present in the *system* (e. g. in the LD_LIBRARY_PATH), so it does not make much sense to give a specific directory for it. – Alfe Nov 30 '14 at 01:07

2 Answers2

31

now from what i can see the command is -l + filename, for example my filename is directory/libtest.so it would be -ldirectory/libtest.so

No, that's not correct. It should be -Ldirectory -ltest i.e. you use -L to add a directory to the search paths where the linker will look for libraries, and you say which libraries to link to with -l, but to link to libtest.so or libtest.a you say -ltest without the lib prefix or the file extension.

You can link by naming the file explicitly, without -L or -l options, i.e. just directory/libtest.so, but for dynamic libraries that is almost always the wrong thing to do, as it embeds that exact path into the executable, so the same library must be in the same place when the program runs. You typically want to link to it by name (not path) so that the library with that name can be used from any location at run-time.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • Well it's pretty clearly documented if you bother to read the GCC docs https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html – Jonathan Wakely Nov 30 '14 at 01:52
  • 1
    Also be aware that you won't _include_ the .so in your own .so, you will just link to it, making your .so depend on the other one, not include its contents. – Jonathan Wakely Nov 30 '14 at 01:53
  • Hey @JonathanWakely, I think I did exactly what you said. https://stackoverflow.com/questions/47967646/link-a-shared-library-with-g yet still ran into an error. Can you shed some lights please? – Emma He Dec 26 '17 at 09:54
2

This is a step by step procedure of how to create and link a .so with .cpp file

  1. Create .cpp file that you want to convert to .so.
    Example -
    #include<stdio.h> int add(int a , int b) { return a+b;}

    Save it by name add.cpp

  2. Create .so with following command
    gcc -c -fPIC add.cpp -o add.o

    This creates libadd.so

  3. Create a .cpp file which will use this .so file
    Example-
    #include<stdio.h> extern int add(int a, int b); int main(int argc, char** argv) { printf("Hello the output is %d \n",add(10,15)); return 0; }

    Save it as main_file.cpp

  4. Create a .o file from this file using this command
    g++ -c main_file.cpp

  5. Link .so with .o using this command
    g++ -o prog main_file.o -L. -ladd

    Here L specifies the folder with the .so file
    And -l specifies the name of the .so library

  6. Run the program with the command
    ./prog

Shubham Arora
  • 807
  • 9
  • 10