单命令编译和链接失败,单独的步骤工作 [英] Single-command compile and link fails, separate steps work

查看:133
本文介绍了单命令编译和链接失败,单独的步骤工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图用g ++解决链接器问题<​​/a>时,我发现试图在一个命令中编译链接一个简单的单文件程序失败,由于未定义的符号。

  g ++ -lEGL -lGLESv2 -o test test.cpp 

但是,如果我单独编译test.cpp,然后链接为第二步,一切正常。

  g ++ -c test.cpp 
g ++ -o test test.o -lGL -lGLESv2

第一个命令和其他命令之间有什么区别,为什么单向失败和其他方式工作?我猜这是与链接顺序有关的,但我觉得这是一个小错误。

当您一次编译和链接时, pre> g ++ -lEGL -lGLESv2 -o test test.cpp

g ++ 服从如下:

  g ++ -c -o deleteme.o test.cpp 
g ++ -lEGL -lGLESv2 -o test deleteme.o
rm deleteme.o

(如果使用 -v (详细)选项运行该命令并小心仔细检查
goobledegook,则可以发现编译器,汇编器和链接器的不同调用
,并在其间传递临时文件)。


现在你看到了什么问题?这是库搜索顺序。在链接中:

  g ++ -lEGL -lGLESv2 -o test deleteme.o 

您要告诉链接器搜索 libEGL libGLESv2 为未解析的符号
,在读取目标文件之前, deleteme.o 需要它们中的符号,所以这些符号
将不会被解析。在你的第二次联系中:

  g ++ -o test test.o -lGL -lGLESv2 

您的链接顺序正确。这里没有任何车。从 man ld


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


While I was trying to solve a linker problem with g++, I found that trying to compile link a simple one-file program in one command was failing, due to undefined symbols.

g++ -lEGL -lGLESv2 -o test test.cpp

However, if I compiled test.cpp separately, and then linked as a second step, everything works OK.

g++ -c test.cpp
g++ -o test test.o -lGL -lGLESv2

What's the difference between the first command and the others, and why would one way fail and the other way work? I'm guessing it's something to do with the order of linking, but I get the feeling that this is a little buggy.

解决方案

When you compile and link in one go, as per:

g++ -lEGL -lGLESv2 -o test test.cpp

g++ obeys as if you did:

g++ -c -o deleteme.o test.cpp
g++ -lEGL -lGLESv2 -o test deleteme.o
rm deleteme.o

(If you run the command with the -v (verbose) option and scrutinize the goobledegook carefully, you'll be able to spot the distinct invocations of the compiler, assembler, and linker, with temporary files passed between).

So now do you see what's wrong? It's library search order. In the linkage:

g++ -lEGL -lGLESv2 -o test deleteme.o

You are telling the linker to search libEGL and libGLESv2 for unresolved symbols before reading the object file, deleteme.o that requires symbols from them, so those symbols will go unresolved. In your second linkage:

g++ -o test test.o -lGL -lGLESv2

You've got the linkage order right. There's nothing buggy here. From man ld

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.

这篇关于单命令编译和链接失败,单独的步骤工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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