如何在静态库中编译程序?

Posted

技术标签:

【中文标题】如何在静态库中编译程序?【英文标题】:How can i compile program in static library? 【发布时间】:2016-06-17 07:31:07 【问题描述】:

我用php-cpp

并创建程序并在共享库中正常工作,Makefile 为 Here

但我想用静态库编译

所以我使用了这个命令:

/opt/rh/devtool/opt/rh/devtoolset-3/root/usr/bin/g++ -c -std=c++11 md5.cpp -o md5.o /opt/rh/devtool/opt/rh/devtoolset-3/root/usr/bin/g++ -c -std=c++11 base64.cpp -o base64.o /opt/rh/devtool/opt/rh/devtoolset-3/root/usr/bin/g++ -c -std=c++11 main.cpp -o main.o

并使用ar rcs my_lib.a main.o base64.o md5.o 创建库

用于编译

/opt/rh/devtoolset-3/root/usr/bin/g++ -std=c++11 -o my_prog.o main.cpp base64.cpp md5.cpp my_lib.a

但返回错误:

/usr/lib/../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
/tmp/ccYj8JlX.o: In function `parsekit(Php::Parameters&)':
main.cpp:(.text+0x246): undefined reference to `Php::SERVER'
main.cpp:(.text+0x276): undefined reference to `Php::Value::~Value()'
main.cpp:(.text+0x82b): undefined reference to `Php::out'
main.cpp:(.text+0x851): undefined reference to `Php::Value::Value(bool)'
main.cpp:(.text+0x8f1): undefined reference to `Php::out'
main.cpp:(.text+0x917): undefined reference to `Php::Value::Value(bool)'
main.cpp:(.text+0xab2): undefined reference to `Php::Value::Value(std::string const&)'
main.cpp:(.text+0xb51): undefined reference to `Php::Value::~Value()'
/tmp/ccYj8JlX.o: In function `get_module':
main.cpp:(.text+0xe79): undefined reference to `Php::Extension::Extension(char const*, char const*, int)'
main.cpp:(.text+0xe92): undefined reference to `Php::Extension::~Extension()'
main.cpp:(.text+0xecc): undefined reference to `Php::Namespace::add(char const*, Php::Value (* const&)(Php::Parameters&), std::initializer_list<Php::Argument> const&)'
/tmp/ccYj8JlX.o: In function `Php::Value::operator std::string() const':
main.cpp:(.text._ZNK3Php5ValuecvSsEv[_ZNK3Php5ValuecvSsEv]+0x1f): undefined reference to `Php::Value::stringValue() const'
/tmp/ccYj8JlX.o: In function `Php::Super::operator[](char const*)':
main.cpp:(.text._ZN3Php5SuperixEPKc[_ZN3Php5SuperixEPKc]+0x24): undefined reference to `Php::Super::value()'
main.cpp:(.text._ZN3Php5SuperixEPKc[_ZN3Php5SuperixEPKc]+0x3d): undefined reference to `Php::Value::get(char const*, int) const'
main.cpp:(.text._ZN3Php5SuperixEPKc[_ZN3Php5SuperixEPKc]+0x49): undefined reference to `Php::Value::~Value()'
main.cpp:(.text._ZN3Php5SuperixEPKc[_ZN3Php5SuperixEPKc]+0x5a): undefined reference to `Php::Value::~Value()'
/tmp/ccYj8JlX.o: In function `Php::Extension::operator void*()':
main.cpp:(.text._ZN3Php9ExtensioncvPvEv[_ZN3Php9ExtensioncvPvEv]+0x14): undefined reference to `Php::Extension::module()'
collect2: error: ld returned 1 exit status

如何使用静态库进行编译?

【问题讨论】:

【参考方案1】:

你还需要在phpcpp中链接

【讨论】:

我该怎么办?请帮忙:(【参考方案2】:

您遇到的错误表明链接器找不到 php-cpp 库(您的编译代码引用了它的符号)。您必须使用 -l 标志指定库路径,然后像这样链接它:Mixing static libraries and shared libraries

【讨论】:

我使用 /opt/rh/devtoolset-3/root/usr/bin/g++ -std=c++11 -shared -fpic -lphpcpp -o my_prog.o main.cpp base64.cpp md5.cpp my_lib.a 并编译,但不是静态的,并且不适用于其他操作系统 -fpic 用于创建共享库,您必须使用-static(需要将 php-cpp 也编译为静态) 你是否安装了静态版本的phpcpp? 我使用了/opt/rh/devtoolset-3/root/usr/bin/g++ -std=c++11 -static -lphpcpp -o my_prog.o main.cpp base64.cpp md5.cpp my_lib.a,但在我的帖子中再次返回错误 我安装了这个文件php-cpp.com/documentation/install我不知道是静态的还是共享的

以上是关于如何在静态库中编译程序?的主要内容,如果未能解决你的问题,请参考以下文章

gcc找不到静态库中的函数

如何在 UWP 类库中使用 XAML 静态资源和设计时数据

如何更改静态链接库中 const 字符串数组的 Visual Studio C++ 初始化序列

如何让静态库中的可执行程序不调用的函数不链接进该可执行程序?(-ffunction-sections -Wl,--gc-sections)

如何列出具有 .o 扩展名的静态库中的文件名

静态库中的 CoreData 自动迁移