将GMP静态链接到OpenMP [英] linking OpenMP statically with GCC
问题描述
#include< stdio.h>
int main(){
printf(asdf\\\
);
}
我可以像这样静态链接
g ++ -static print.cpp
或者像这样
g ++ -static-libgcc -Wl,-Bstatic -lc print.cpp -o print
但现在让我们添加一个OpenMP并调用文件print_omp.cpp
#include
#include< stdio.h>
int main(){
printf(%d \ n,omp_get_num_threads());
}
我可以像这样静态链接(我用 ldd
)
g ++ -fopenmp -static print_omp.cpp
然而,这不起作用
g ++ -fopenmp -static-libgcc -Wl,-Bstatic -lc print_omp.cpp -o print
我尝试了各种 -Wl, - whole-archive -lpthread -Wl, - -no-whole-archive 和-lgomp -lpthread但没有运气(我遇到了各种连接到pthread的问题)。有人可以解释我如何在不使用 -static
选项的情况下做到这一点吗? //gcc.gnu.org/onlinedocs/gfortran/OpenMP.htmlrel =nofollow> GCC says
在基于glibc的系统上,由于底层pthreads-implementation的局限性,启用OpenMP的应用程序无法静态链接。
然而,由于 g ++ -fopenmp -static print_omp.cpp
工作得很好,这对我没有任何意义。
编辑:
我明白了这一点。库GOMP带有GCC,而pthreads和libc来自GLIBC。所以我可以像这样静态链接GOMP
ln -s`g ++ -print-file-name = libgomp.a`
g ++ foo.cpp -static-libgcc -static-libstdc ++ -L。 -o foo -O3 -fopenmp
ldd显示
linux-vdso.so.1 => (0x00007fff71dbe000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0(0x00007fc231923000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6(0x00007fc23155c000)
/lib64/ld-linux-x86-64.so.2(0x00007fc231b5c000)
但是,如果我试试这个
ln - s`g ++ -print-file-name = libpthread.a`
g ++ foo.cpp -static-libgcc -static-libstdc ++ -L。 -o foo -O3 -fopenmp
它不会链接。 Pthreads和libc必须静态链接在一起。因此,一旦我添加
ln -s`g ++ - print-file-name = libc.a`
g ++ foo.cpp -static-libgcc -static-libstdc ++ -L。 -o foo -O3 -fopenmp
ldd返回
不是动态可执行文件
我真的不知道为什么你可能只想静态链接 libgomp
,但有单独的编译和链接命令可能会有所帮助。例如,假设 main.cpp
包含:
#include #include< stdio.h>
int main(){
#pragma omp parallel
{
printf(%d\\\
,omp_get_thread_num());
$ / code>
然后:
〜/ tmp $ ls
main.cpp
〜/ tmp $ g ++ -Wall -Werror -pedantic -fopenmp main.cpp -c
〜/ tmp $ ls
main.cpp main.o
〜/ tmp $ locate libgomp.a
$ {SOME_PATH_TO_LIBGOMP} /libgomp.a
〜/ tmp $ g ++ -Wall -Werror -pedantic main.o -o main.x $ {SOME_PATH_TO_LIBGOMP} /libgomp.a -pthread
〜/ tmp $ ls
main.cpp main.o main.x
〜/ tmp $ ldd main.x
linux-gate.so.1 => (0xb7747000)
libstdc ++。so.6 => /production/install/gnu/compiler/gcc/lib/libstdc++.so.6(0xb765c000)
libm.so.6 => /lib/i386-linux-gnu/libm.so.6(0xb75fa000)
libgcc_s.so.1 => /production/install/gnu/compiler/gcc/lib/libgcc_s.so.1(0xb75de000)
libpthread.so.0 => /lib/i386-linux-gnu/libpthread.so.0(0xb75c2000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6(0xb7413000)
/lib/ld-linux.so.2(0xb7748000)
Given the following file print.cpp
#include <stdio.h>
int main() {
printf("asdf\n");
}
I can link this statically like this
g++ -static print.cpp
or like this
g++ -static-libgcc -Wl,-Bstatic -lc print.cpp -o print
But now let's add a little OpenMP and call the file print_omp.cpp
#include <omp.h>
#include <stdio.h>
int main() {
printf("%d\n", omp_get_num_threads());
}
I can link this statically like this (I checked it with ldd
)
g++ -fopenmp -static print_omp.cpp
However, this does not work
g++ -fopenmp -static-libgcc -Wl,-Bstatic -lc print_omp.cpp -o print
I have tried various combinations of -Wl,--whole-archive -lpthread -Wl,--no-whole-archive and -lgomp -lpthread but no luck (I get various problems linking to pthreads). Can someone explain how I can do this without using the -static
option?
On glibc-based systems, OpenMP enabled applications cannot be statically linked due to limitations of the underlying pthreads-implementation
However, since g++ -fopenmp -static print_omp.cpp
works just fine this does not make sense to me.
Edit: I figured this out. The library GOMP comes with GCC whereas pthreads and libc come from GLIBC. So I can link GOMP statically like this
ln -s `g++ -print-file-name=libgomp.a`
g++ foo.cpp -static-libgcc -static-libstdc++ -L. -o foo -O3 -fopenmp
ldd shows
linux-vdso.so.1 => (0x00007fff71dbe000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc231923000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc23155c000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc231b5c000)
However, if I the try this
ln -s `g++ -print-file-name=libpthread.a`
g++ foo.cpp -static-libgcc -static-libstdc++ -L. -o foo -O3 -fopenmp
It won't link. Pthreads and libc must be linked statically together. So once I add
ln -s `g++ -print-file-name=libc.a`
g++ foo.cpp -static-libgcc -static-libstdc++ -L. -o foo -O3 -fopenmp
ldd returns
not a dynamic executable
I really don't get why you may want to link only libgomp
statically, but having separate compilation and linking commands may help. For instance assume main.cpp
contains:
#include <omp.h>
#include <stdio.h>
int main() {
#pragma omp parallel
{
printf("%d\n", omp_get_thread_num());
}
}
Then:
~/tmp$ ls
main.cpp
~/tmp$ g++ -Wall -Werror -pedantic -fopenmp main.cpp -c
~/tmp$ ls
main.cpp main.o
~/tmp$ locate libgomp.a
${SOME_PATH_TO_LIBGOMP}/libgomp.a
~/tmp$ g++ -Wall -Werror -pedantic main.o -o main.x ${SOME_PATH_TO_LIBGOMP}/libgomp.a -pthread
~/tmp$ ls
main.cpp main.o main.x
~/tmp$ ldd main.x
linux-gate.so.1 => (0xb7747000)
libstdc++.so.6 => /production/install/gnu/compiler/gcc/lib/libstdc++.so.6 (0xb765c000)
libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xb75fa000)
libgcc_s.so.1 => /production/install/gnu/compiler/gcc/lib/libgcc_s.so.1 (0xb75de000)
libpthread.so.0 => /lib/i386-linux-gnu/libpthread.so.0 (0xb75c2000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb7413000)
/lib/ld-linux.so.2 (0xb7748000)
这篇关于将GMP静态链接到OpenMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!