我有一个简单的HelloWorld.c程序(所以,我把它放在这里),我正在尝试使用启用了LTO的clang版本3.3构建,这会引发奇怪的错误。我正在用cmake
使用-DCMAKE_TOOLCHAIN_FILE
构建它。我可以在没有-flto
的情况下成功构建和链接这个程序,但我需要能够使用这个限定符。工具链适用于TileGX(因此需要保留在clang版本3.3)。所以这是我的环境:
Ubuntu 16.04
cmake Version 3.9.4
I built the TileGX toolchain on 16.04, as well as 14.04
I build clang Version 3.3 on 16.04 (can't build it on 14.04)
这是我的CMakeLists.txt
文件:
cmake_minimum_required (VERSION 3.9)
project (HelloWorld)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -v -flto -static")
add_executable(HelloWorld HelloWorld.c)
我添加了-v
标志,所以看看clang中发生了什么。
clang尝试链接的输出是(重新格式化以便于阅读):
clang version 3.3
Target: tilegx-unknown-linux-gnu
Thread model: posix
./toolchains/univ_tilegx/usr/bin/tilegx-unknown-linux-gnu-ld -m elf64tilegx \
-static -o HelloWorld \
./toolchains/univ_tilegx/usr/tilegx-unknown-linux-gnu/sys-root/usr/lib/crt1.o \
./toolchains/univ_tilegx/usr/tilegx-unknown-linux-gnu/sys-root/usr/lib/crti.o \
./toolchains/univ_tilegx/usr/lib/gcc/tilegx-unknown-linux-gnu/4.9.2/crtbeginT.o \
-L./toolchains/univ_tilegx/usr/tilegx-unknown-linux-gnu/lib \
-L./third_party/toolchains/univ_tilegx/usr/lib/gcc/tilegx-unknown-linux-gnu/4.9.2/ \
-L./toolchains/univ_tilegx/usr/tilegx-unknown-linux-gnu/lib \
-L./toolchains/univ_tilegx/usr/lib/gcc/tilegx-unknown-linux-gnu/4.9.2/ \
-L/lib/../lib64 -L/lib -L/usr/lib -plugin ../lib/LLVMgold.so \
CMakeFiles/HelloWorld.dir/HelloWorld.c.o \
--start-group \
-lgcc -lgcc_eh -lc
--end-group \
./toolchains/univ_tilegx/usr/lib/gcc/tilegx-unknown-linux-gnu/4.9.2/crtend.o \
./toolchains/univ_tilegx/usr/tilegx-unknown-linux-gnu/sys-root/usr/lib/crtn.o
./toolchains/univ_tilegx/usr/bin/tilegx-unknown-linux-gnu-ld: cannot find 0�: \
No such file or directory
./toolchains/univ_tilegx/usr/bin/tilegx-unknown-linux-gnu-ld: error:
Failed to delete '0�': 0�: can't get status of file: No such file or directory
clang-3.3: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/HelloWorld.dir/build.make:94: recipe for target 'HelloWorld' failed
make[2]: *** [HelloWorld] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/HelloWorld.dir/all' failed
make[1]: *** [CMakeFiles/HelloWorld.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
起初我认为这是一个使用基于Ubuntu 16.04构建的可共享对象(LLVMgold.so
)以及基于14.04构建的链接器的问题。链接器将调用可共享对象,然后该对象将回调到链接器。但是,既然我有一个基于16.04构建的链接器,我仍然会收到错误。
自从我构建LLVMgold.so
以来,我添加了一些代码来跟踪链接器调用可共享对象时发生了什么,但是一切看起来都很好。我可以看到ld
找不到一个奇怪命名的文件,但我不确定它们在提供的那组文件中可能是哪个文件。
我已经尝试了所有我能想象到的东西,以便我可以在这种环境中使用-flto
。谁看过这个吗?有解决方案吗?还有其他我可以或应该做的事情吗?
好的,这是一个类具有指向字符串的指针的情况,它将复制到局部变量。当类被销毁时,指针也会消失,因此局部变量现在有效地指向随机存储器。解决方法是将字符串复制到局部变量中,然后记得在完成后将其释放。
对于任何关心的人来说,将局部变量中的strdup调用到自身就足够了。唯一需要的代码更改是在gold-plugin.cpp
。