[英]Compiling with g++ and having GraphicsMagick++ as static library
The following command works 以下命令有效
g++ file.cpp $( GraphicsMagick++-config --cppflags --cxxflags --ldflags --libs )
but i want a static executable that is not linked to the library, so i have tried 但是我想要一个没有链接到库的静态可执行文件,所以我尝试了
g++ file.cpp $( GraphicsMagick++-config --cppflags --cxxflags --ldflags --libs ) -static
but it only generates the following errors 但它只会产生以下错误
/usr/bin/ld.bfd.real: /usr/lib/gcc/x86_64-linux-gnu/4.6/crtbeginT.o: relocation R_X86_64_32 against `__DTOR_END__' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtbeginT.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
adding -fPIC
gives the same result. 添加-fPIC
得到相同的结果。
I'm using the GraphicsMagick++ version that comes with the standard repository in Ubuntu 12.04 64 bit. 我正在使用Ubuntu 12.04 64位标准存储库随附的GraphicsMagick ++版本。
Since GraphicsMagick++ is a fork of ImageMagick some old trick for ImageMagick will probably work but i have never used ImageMagick, only GraphicsMagick++. 由于GraphicsMagick ++是ImageMagick的分支,因此ImageMagick的一些老技巧可能会起作用,但我从未使用过ImageMagick,只有GraphicsMagick ++。
To link statically against that particular library, use -Wl,-Bstatic
before it, ie: 要针对该特定库进行静态链接-Wl,-Bstatic
在其之前使用-Wl,-Bstatic
,即:
g++ $( GraphicsMagick++-config --cppflags --cxxflags ) -fPIC file.cpp -o file -Wl,-Bstatic $( GraphicsMagick++-config --ldflags --libs )
If you then wanted to pass more libraries, and link dynamically against them, you can pass -Wl,-Bdynamic
to switch back and add more libraries afterwards. 如果然后您想要传递更多的库,并针对它们动态链接,则可以传递-Wl,-Bdynamic
来切换回去,然后再添加更多的库。
Edit : Also note the specific argument order. 编辑 :还请注意特定的参数顺序。 First compiler flags, then file, then linker flags and libraries at the end. 首先是编译器标志,然后是文件,然后是链接器标志和库。 With other order, you can run into random failures. 使用其他顺序,您可能会遇到随机故障。 Really. 真。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.