在Ubuntu上如何手动链接g++库

3

我正在尝试在Ubuntu上手动链接外部库与C++。由于这是我的第一个C++项目,我缺少一些基础知识。 这是我的代码(这个代码尝试连接到Spread):

#include <iostream>
#include <sp.h>

using namespace std;

int main() {
    int status;
    status = SP_connect("4803@localhost", "test1", 0, 0, 0, 0);
    cout << "done\n";
    return 0;
}

如果我只是尝试运行它

user@computer:~/thesis$ g++ -o example1 test.cpp
/tmp/cczPLZQ0.o: In function `main':
test.cpp:(.text+0x39): undefined reference to `SP_connect'
collect2: ld returned 1 exit status

据我所知,我需要链接一个库,我尝试使用-l来实现。
kristjan@kupo:~/thesis$ g++ -o example1 test.cpp -llibspread
/usr/bin/ld: cannot find -llibspread
collect2: ld returned 1 exit status

我也试过这个方法(纯属瞎猜):
user@computer:~/thesis$ g++ -o example1 test.cpp $(pkg-config -cflags /usr/local/lib/libspread) $(pkg-config --libs /usr/local/lib/libspread)

-cflags: unknown option
Package /usr/local/lib/libspread was not found in the pkg-config search path.
Perhaps you should add the directory containing `/usr/local/lib/libspread.pc'
to the PKG_CONFIG_PATH environment variable
No package '/usr/local/lib/libspread' found
/tmp/ccU8GTC2.o: In function `main':
test.cpp:(.text+0x39): undefined reference to `SP_connect'
collect2: ld returned 1 exit status

任何帮助都将不胜感激。


6
如果.so文件在标准位置,则-lspread是正确的选项。 - Mat
1
你应该跳过-llibspread中的lib,只使用-lspread。 - gulyan
1个回答

2
如@Mat所说,你应该使用-lspread并省略前缀lib

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接