How to create a dynamic library for c++ on linux?

风流意气都作罢 提交于 2019-12-13 05:13:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!