使用 G++ 强制链接到 pthread
Force linking to pthread with G++
我的一个项目遇到了一些问题。看来我 link 想要一个需要 pthread 符号的库,但库本身并没有 link 使用 pthread。症状是当我尝试 运行 时程序出现段错误。我运行:
LD_DEBUG=all ./my_program
并在查找 pthread_create 时发现它存在段错误。如果我使用此命令强制预加载 pthread,程序 运行s:
LD_PRELOAD=/lib/x86_64-linux-gnu/libpthread.so.0 ./my_program
为了解决这个问题,我想我只是 link my_program 在使用 -lpthread 的构建脚本中使用 pthread。那没有用,所以我尝试了-pthread。也没用。我能够将其重现到最低限度:
echo "int main() { return 0; }" | g++ -x c++ - -pthread -lpthread -o my_program && ldd my_program
linux-vdso.so.1 => (0x00007ffe4fd08000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f150ca5c000)
/lib64/ld-linux-x86-64.so.2 (0x000055ac8c275000)
正如您从 ldd 的输出中看到的那样,my_program 未 link 使用 pthread。我怀疑发生这种情况是因为 g++ 知道我实际上并没有使用 pthread 中的任何符号,也没有使用它 link。所以我尝试添加对 pthread_create:
的调用
echo -e "#include <pthread.h>\n int main() { pthread_create(0,0,0,0); return 0; }" | g++ -x c++ - -pthread -o my_program && ldd my_program
<stdin>: In function ‘int main()’:
<stdin>:2:37: warning: null argument where non-null required (argument 1) [-Wnonnull]
<stdin>:2:37: warning: null argument where non-null required (argument 3) [-Wnonnull]
linux-vdso.so.1 => (0x00007ffd977f9000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fafe1bb6000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fafe17f1000)
/lib64/ld-linux-x86-64.so.2 (0x0000563ee0002000)
请忽略警告,我知道对 pthread_create 的调用是虚假的。关键是现在它实际上 links 与 pthread。
所以我的问题是:是否有强制 g++ link 与某些东西(在本例中为 pthread)而不使用符号添加虚拟代码?
Is there anyway to force g++ to link with something (in this case pthread) without adding dummy code using the symbols?
试试这个:
echo "int main() { return 0; }" |
g++ -x c++ - -o my_program -Wl,--no-as-needed -lpthread -Wl,--as-needed
我的一个项目遇到了一些问题。看来我 link 想要一个需要 pthread 符号的库,但库本身并没有 link 使用 pthread。症状是当我尝试 运行 时程序出现段错误。我运行:
LD_DEBUG=all ./my_program
并在查找 pthread_create 时发现它存在段错误。如果我使用此命令强制预加载 pthread,程序 运行s:
LD_PRELOAD=/lib/x86_64-linux-gnu/libpthread.so.0 ./my_program
为了解决这个问题,我想我只是 link my_program 在使用 -lpthread 的构建脚本中使用 pthread。那没有用,所以我尝试了-pthread。也没用。我能够将其重现到最低限度:
echo "int main() { return 0; }" | g++ -x c++ - -pthread -lpthread -o my_program && ldd my_program
linux-vdso.so.1 => (0x00007ffe4fd08000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f150ca5c000)
/lib64/ld-linux-x86-64.so.2 (0x000055ac8c275000)
正如您从 ldd 的输出中看到的那样,my_program 未 link 使用 pthread。我怀疑发生这种情况是因为 g++ 知道我实际上并没有使用 pthread 中的任何符号,也没有使用它 link。所以我尝试添加对 pthread_create:
的调用echo -e "#include <pthread.h>\n int main() { pthread_create(0,0,0,0); return 0; }" | g++ -x c++ - -pthread -o my_program && ldd my_program
<stdin>: In function ‘int main()’:
<stdin>:2:37: warning: null argument where non-null required (argument 1) [-Wnonnull]
<stdin>:2:37: warning: null argument where non-null required (argument 3) [-Wnonnull]
linux-vdso.so.1 => (0x00007ffd977f9000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fafe1bb6000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fafe17f1000)
/lib64/ld-linux-x86-64.so.2 (0x0000563ee0002000)
请忽略警告,我知道对 pthread_create 的调用是虚假的。关键是现在它实际上 links 与 pthread。
所以我的问题是:是否有强制 g++ link 与某些东西(在本例中为 pthread)而不使用符号添加虚拟代码?
Is there anyway to force g++ to link with something (in this case pthread) without adding dummy code using the symbols?
试试这个:
echo "int main() { return 0; }" |
g++ -x c++ - -o my_program -Wl,--no-as-needed -lpthread -Wl,--as-needed