问题
I would like to create a dynamic library for c++ program on linux. In c++ program/system I`m using libconfig++ library, libpqxx library, some boost and c++11.
My steps: 1)
g++ -Wall -I/usr/local/include/ -std=c++0x -lconfig++ -Wall -lpqxx -lpq -fPIC -c ../SourceFiles/DBHandler.cpp ../SourceFiles/ParamServer.cpp ../SourceFiles/Functions.cpp
2)
g++ -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 *.o
3)
ln -sf libctest.so.1.0 libctest.so.1
4)
ln -sf libctest.so.1.0 libctest.so
5) compile
g++ -Wall -I/path/to/include-files -L/path/to/libraries program.cpp -I/usr/local/include/ -std=c++0x -lconfig++ -lpqxx -lpq -lctest -o prog
After execute above command :
/usr/bin/ld: cannot find -lctest
collect2: ld returned 1 exit status
What am I doing wrong?
Here is the reference: enter link description here
回答1:
In step 5, you forgot -L.
to look for libraries in the current directory.
By default, only a [long] list of system directories is used when searching for libraries.
You will also need to add .
to the LD_LIBRARY_PATH
environment variable before executing your program, so that the current directory is searched at runtime, too. Running ldconfig
will avoid this, but if you are only testing your library and do not want to persistently affect your system, I would stick to the LD_LIBRARY_PATH
approach.
An alternative is to "install" your library into one of those directories, such as /usr/local/lib
(or your equivalent). You should use ldconfig
after doing this, so that the dynamic library cache and all your symlinks are set up for you. This is the canonical approach but may not be suitable during iterative development of said library.
回答2:
You need to ldconfig
update the dynamic library cache -- it will also create the symbolic links for you.
See eg Section 3.5 of this Linux Documentation Project HOWTO
来源:https://stackoverflow.com/questions/22001017/how-to-create-a-dynamic-library-for-c-on-linux