isocppforbids
isocppforbids

Reputation: 389

My program cannot find my shared library during execution

My program cannot find my shared library during execution.
That's how I compiled everything:

//shared library fct
g++ -c -fpic fct.cpp
g++ -shared fct.o -o libfct.so

//my program
g++ main.cpp -L/home/user/shared_library/ -lfct -I/home/user/shared_library/ -o main

When I tried to run the program, it gives me this error message:

./main: error while loading shared libraries: libfct.so: cannot open shared object file: No such file or directory

here's my source files:

main.cpp:

#include <iostream>
#include "fct.h"

int main()
{
   fct();

   return 0;
}

fct.h:

#ifndef FCT_HEADER_INCLUDED
#define FCT_HEADER_INCLUDED

#include <iostream>

void fct();

#endif

fct.cpp:

#include "fct.h"

void fct()
{
    std::cout << "fct() was called!\n";
}

Upvotes: 1

Views: 510

Answers (1)

Guy Marino
Guy Marino

Reputation: 439

The shared library path is needed twice, both at compile time and runtime. Passing the library path to the compiler does not guarantee that the linker will be able to find it. For a temporary solution, you can add home/user/shared_library to the LD_LIBRARY_PATH environment variable (on Linux & macOS). For a more permanent solution, you should install it in a directory on the default dynamic linker search path.

Upvotes: 1

Related Questions