I was trying to compile an unrelated project when I got an undefined reference when using a specific function of the Raylib library.
当我使用Raylib库的特定函数时,当我尝试编译一个不相关的项目时,得到了一个未定义的引用。
I was able to reproduce this behaviour using the following test.c
我能够使用下面的测试重现这种行为。
#include <raylib.h>
int main(){
InitWindow(500,500, "test");
int w = GetRenderWidth(); // undefined reference symbol
while(!WindowShouldClose()){
BeginDrawing();
ClearBackground(BLACK);
EndDrawing();
}
CloseWindow();
return 0;
}
This code was compiled using the following command.
此代码是使用以下命令编译的。
gcc test.c -o app -L/usr/local/lib -lraylib
The resulting output was
产生的输出是
/usr/bin/ld: /tmp/ccxqgamX.o: in function `main':
code.c:(.text+0x27): undefined reference to `GetRenderWidth'
collect2: error: ld returned 1 exit status
Once I commented the offending line out however (// int w = GetRenderWidth();
)
The app compiled fine and also was able run succesfully.
然而,一旦我注释掉了令人不快的一行(//int w=GetRenderWidth();),应用程序编译得很好,也能够成功运行。
I examined the libraylib.a
library in my /usr/local/lib
folder using
我使用以下命令检查了/usr/local/lib文件夹中的libraylib.a库
nm /usr/local/lib/libraylib.a | grep GetRenderWidth
This resulted in the following outputt:
这导致了以下输出:
0000000000027b1e T GetRenderWidth
From this I surmise that the symbol is actually present in the library I linked and thus my question.
Why does my compiler (both gcc
and clang
) report an undefined reference on GetRenderWidth()
but not on other symbols from the same library?
由此,我推测该符号实际上存在于我所链接的库中,因此我的问题也是如此。为什么我的编译器(GCC和clang)在GetRenderWidth()上报告一个未定义的引用,而不是同一个库中的其他符号?
更多回答
Do you have multiple installs of Raylib? Perhaps one that is searched and used before the one in /usr/local/lib
? When building, if you remove the -L/usr/local/lib
option, does it still find the Raylib library?
你有多个版本的Raylib吗?也许是在/usr/local/lib中的搜索和使用之前搜索和使用的那个?在构建时,如果删除-L/usr/local/lib选项,它是否仍能找到Raylib库?
What does width: height: title:
do? Are they an artifact of the code editor?
Width:Height:TITLE:做什么?它们是代码编辑器的产物吗?
@Someprogrammerdude yes it still displays the same behaviour including compiling and running when the GetRenderWidth function call is removed.
@SomeProgram是的,它仍然显示相同的行为,包括在删除GetRenderWidth函数调用时编译和运行。
@WeatherVane yes sorry that is because I use neoclide with neovim so when I copied the text it copied everything neovim was displaying, I have removed it now.
@weathervane是的,对不起,这是因为我使用了neoclide和nevim,所以当我复制文本时,它复制了nevim显示的所有内容,现在我已经删除了它。
"Why does my compiler (both gcc and clang) report an undefined reference on GetRenderWidth()
but not on other symbols from the same library?" -- because the version of libraylib being linked does not provide an external definition of function GetRenderWidth()
. Since the one you are examining does define one, you must be linking a different library. Possibly there's a shared-library version (libraylib.so
) somewhere that is being linked in preference to the static one.
“为什么我的编译器(GCC和clang)在GetRenderWidth()上报告了一个未定义的引用,而不是同一个库中的其他符号?”--因为要链接的libraylib版本不提供GetRenderWidth()函数的外部定义。因为您正在检查的库确实定义了一个库,所以您必须链接一个不同的库。可能有一个共享库版本(libraylib.so)在某个地方优先被链接,而不是静态版本。
我是一名优秀的程序员,十分优秀!