basickarl
basickarl

Reputation: 40484

Link .so file to .cpp file via g++ compiling

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

Upvotes: 17

Views: 50028

Answers (3)

angstyloop
angstyloop

Reputation: 327

pkg-config

I like to use pkg-config --cflags --libs libtest to generate the compiler flags and linker flags for me. The source code installation just needs to provide a .pc file, and put it in a directory that's listed in PKG_CONFIG_PATH, e.g., /usr/local/lib/pkgconfig. You can use it like this:

g++ -o myprogram myprogam.c pkg-config --cflags --libs libtest`

Rebuilding the linker cache

Even if LD_CONFIG_PATH contains /usr/local/lib, and your shared object library ( the .so file ) is located there, you still might get an error like

error while loading shared libraries: <INSERT LIBRARY NAME>.so.0: cannot open shared object file: No such file or directory

If your linker cache /etc/ld.so.cache has not been rebuilt since the library was installed.

You can fix this by running ldconfig ( or sudo ldconfig, depending on the /etc directory permissions, which are likely only writeable by the root user ).

When successful, ldconfig prints no output. For more detailed output when running ldconfig, use the -v ( or --verbose ) flag.

If you have ever run into this error, and then it magically fixed itself while you were troubleshooting, it's probably because you followed someone's advice and did something like

ldconfig -v | grep <INSERT LIBRARY NAME>

to make sure the .so file was in a directory listed in LD_LIBRARY_PATH, like /usr/local/lib. It's a reasonable troubleshooting step to perform if you already confirmed that directory is included in /etc/ld.so.conf or /etc/ld.so.conf.d/*.conf, but ldconfig -v still runs ldconfig in verbose mode, which rebuilds /etc/ld.so.cache, and probably fixed your issue.

If your error mysteriously fixed itself after some time without you having to run ldconfig, then something else on your system must have run it since then - e.g. a startup script, a scheduled task, or installation scripts for other software.

Upvotes: 0

Shubham Arora
Shubham Arora

Reputation: 947

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

Upvotes: 7

Jonathan Wakely
Jonathan Wakely

Reputation: 171323

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.

Upvotes: 33

Related Questions