如何创建Linux下C ++动态库? [英] How to create a dynamic library for c++ on linux?

查看:358
本文介绍了如何创建Linux下C ++动态库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Linux上的C ++程序中创建一个动态库。
在C ++程序/系统利用我真的++ libconfig图书馆,图书馆libpqxx,有些升压和C ++ 11。

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.

我的步骤:
1)

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)编译

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?

下面是参考:
在这里输入链接的描述

推荐答案

在第5步,你忘了 -L。来寻找库在当前目录

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.

您还需要添加 LD_LIBRARY_PATH 环境变量,执行程序,从而使之前,在当前目录中搜索在运行时,太。运行 ldconfig的将避免这一点,但如果你只是在测试你的图书馆以及不想持久地影响你的系统,我会坚持到 LD_LIBRARY_PATH 办法。

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.

另一种方法是安装你的图书馆到这些目录之一,如的/ usr / local / lib目录(或同等学历)。这样做之后,您应该使用 ldconfig的,使动态库高速缓存和所有的符号链接设置为您服务。这是典型的方法,但是未必所述文库的迭代发展过程中合适的

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.

这篇关于如何创建Linux下C ++动态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆