按以下代码提供:
#include <stdio.h>
void output()
{
printf("hello \n");
}
int main()
{
output();
return 0;
}当通过以下命令编译上述代码时:
gcc hello.c -shared -fPIC -pie -o libhello.so -Wl,-E生成的libhello.so不仅是一个共享库,而且是一个可执行文件。然而,当gcc改为clang时,如下所示
clang-10 hello.c -shared -fPIC -pie -o libhello.so -Wl,-E该汇编提出以下警告:
clang: warning: argument unused during compilation: '-pie' [-Wunused-command-line-argument]当执行由clang-10编译的libhello.so时,它也崩溃了。
问: 1.可以像gcc那样使用clang编译可运行的共享库吗?
注意:这个问题只是出于我自己的好奇心,我没有遇到任何实际问题。
发布于 2020-05-10 11:04:28
作为clang-10的警告,clang-10不会像GCC编译器那样生成以下内容:
共享库程序header
中的
两种方法都可以手动完成,如下所示
#include <stdio.h>
const char interp_section[] __attribute__((section(".interp"))) = "/lib64/ld-linux-x86-64.so.2";
void output()
{
printf("hello \n");
}
int main()
{
output();
return 0;
}
void _start()
{
printf("hello.c : %s\n", __FUNCTION__);
exit(0);
}但是,最好使用-Wl,-e,YourEntryFunction标志来创建可运行的共享对象,而不是上述问题中提出的方法。
https://stackoverflow.com/questions/61618714
复制相似问题