GCC 动态库连接-l

链接时,有时需要指定链接的库。这里的库是动态库
如我们用gcc来编译一个c++文件时,如c++中引用了c++的标准库时,需要手动指定.
如test.cpp

[root@test 1]# cat test.cpp
#include<iostream>
int main(int argc,char* argv[])
{
    std::cout<<"hello world"<<std::endl;
    return 0;
}

在链接时,发现报错

[root@test 1]# gcc test.cpp -o test
/tmp/ccQYASBP.o: In function `main':
test.cpp:(.text+0x15): undefined reference to `std::cout'
test.cpp:(.text+0x1a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
test.cpp:(.text+0x1f): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cpp:(.text+0x27): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccQYASBP.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x55): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x64): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

链接时发现无法找到std::cout 等。
这时我们使用-l选项指定需要链接的库

[root@test 1]# gcc test.cpp -o test -lstdc++
[root@test 1]# 
[root@test 1]# 
[root@test 1]# ./test
hello world
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

Powered by bytekits.com,汇天下文字,成非凡梦想!!!