奇怪的g ++链接行为取决于参数顺序 [英] strange g++ linking behavior depending on arguments order

查看:179
本文介绍了奇怪的g ++链接行为取决于参数顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在msys上使用g ++编译一个简单的opengl程序。令我惊讶的是,链接器抱怨未定义的引用:

I was trying to compile a simple opengl program on msys using g++. To my surprise the linker was complaining on undefined references:

$ g++ -mwindows -lopengl32 glut_md2.cpp
C:\Users\...\cceQtYAy.o:glut_md2.cpp:(.text+0x67a): undefined reference to `glGenTextures@8'
C:\Users\...\cceQtYAy.o:glut_md2.cpp:(.text+0x696): undefined reference to `glBindTexture@8'
....

google搜索了一段时间之后,我发现问题出现在g ++参数顺序中:

After googling for a while I found that the problem was in g++ arguments order:

$ g++ glut_md2.cpp -mwindows -lopengl32
--- all ok! ---

有趣的是,第一个例子中g ++的正确参数顺序。即:

The interesting thing is that the correct argument orders in g++ is in the first example. That is:

$ g++ --help
Usage: g++.exe [options] file...
....

我错过了什么吗?为什么在文件参数之后移动选项会使编译成功?我在linux上本地编译时从来没有遇到过这个问题......

Am I missing something? Why moving options after the file argument makes a compilation success? I never had this issue when compiling natively on linux...

推荐答案

我碰到这个问题一两次, -L和-l在命令行结尾。 g ++不会链接,它会调用ld并传递参数,ld man: b
$ b

I bumped into this problem once or twice, you should put -L and -l at the end of command line. g++ doesn't link, it invokes ld and pass arguments, ld man:


链接器只会在位置搜索一次存档它在命令行中指定
。如果存档文件定义了符号
,而该符号在命令行上的存档
之前出现的某个对象中未定义,则链接器将从存档文件中包含相应的文件
。但是,在命令行中出现
的对象中的未定义符号不会导致链接器再次搜索
存档。

The linker will search an archive only once, at the location where it is specified on the command line. If the archive defines a symbol which was undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again.

ld - o /lib/crt0.o hello.o -lc

ld -o /lib/crt0.o hello.o -lc

这篇关于奇怪的g ++链接行为取决于参数顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆