[英]g++ custom dynamic library linking error undefined symbol
Ok, I'm looking for a solution for 2 days now. 好的,我现在正在寻找解决方案,持续2天。 I didn't find anything to solve my problems. 我没有找到任何解决我的问题的方法。
What is currently going on? 目前发生了什么事? So, I tried creating a dynamic library (.so) on Linux Mint Maya 13 with g++. 因此,我尝试使用g ++在Linux Mint Maya 13上创建一个动态库(.so)。
foolib.h: foolib.h:
#pragma once
#include <stdio.h>
void foo(
void
);
foolib.cpp: foolib.cpp:
#include "foolib.h"
void foo(
void
)
{
printf ("Hello World!\n");
};
main.cpp: main.cpp:
#include "foolib.h"
int main(
int argc,
char** argv
)
{
foo ();
};
I compiled these files with these instructions: 我按照以下说明编译了这些文件:
libfoo.so: libfoo.so:
g++ -shared -o libfoo.so -fpic foolib.cpp
foo: foo:
g++ main.cpp -o foo -L -lfoo
Creating libfoo.so works without any errors, but foo throws undefined reference ´foo'. 创建libfoo.so的工作没有任何错误,但是foo会引发未定义的引用“ foo”。 I copied sample code from several web pages and tried to compile it, always the same result. 我从几个网页复制了示例代码,并尝试对其进行编译,结果始终相同。
The funny is, I can link do libdl.so (-ldl), load my .so and my function. 有趣的是,我可以链接做libdl.so(-ldl),加载我的.so和我的函数。 What am I doing wrong? 我究竟做错了什么?
I hope I could formulate my question correctly. 我希望我可以正确地提出我的问题。 Please tell me if I didn't. 请告诉我是否没有。 : ) :)
You should use: 您应该使用:
g++ main.cpp -o foo -L./ -lfoo
or 要么
g++ main.cpp -o foo libfoo.so
You state your foo compilation/link is with g++ main.cpp -o foo -L -lfoo
and this is where the problem is. 您声明foo编译/链接是通过g++ main.cpp -o foo -L -lfoo
,这就是问题所在。 The -L
option requires a parameter that gives the linker an additional directory to search for libraries but you have not provided it. -L
选项需要一个参数,该参数为链接程序提供一个用于搜索库的附加目录,但您没有提供该目录。 So in your case, the linker thinks -lfoo
is the name of a directory to search in, not a library to link in. 因此,在您的情况下,链接器认为-lfoo
是要搜索的目录的名称,而不是要链接的库的名称。
Change -L
to -L.
更改-L
到-L.
and it should work. 它应该工作。
See this documentation for more information . 有关更多信息,请参见本文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.