简体   繁体   English

链接静态库与动态库的问题

[英]Problem linking static library vs dynamic library

I've successfully developed a code using a dynamic library. 我已经使用动态库成功开发了代码。 Nowadays I need to move to a static library. 现在我需要转移到静态库。 So I've built the library with make as specified by the author but when I tried to build my program I've faced these messages: 所以我用作者指定的make构建了库,但是当我尝试构建我的程序时,我遇到了这些消息:

/usr/bin/ld: /usr/local/lib/libfft3dmpi.a(remap3d_wrap.o): in function `remap3d_create':
/home/mirco/Scrivania/fftmpi-1Oct18/src/remap3d_wrap.cpp:36: undefined reference to `operator new(unsigned long)'
/usr/bin/ld: /usr/local/lib/libfft3dmpi.a(remap3d_wrap.o): in function `remap3d_create_fortran(int, void**)':
/home/mirco/Scrivania/fftmpi-1Oct18/src/remap3d_wrap.cpp:45: undefined reference to `operator new(unsigned long)'

And so on.. I've tried to specify, in the makefile, the full path of the library or to use -l(name of the library) but I still face the same messages 等等..我试图在makefile中指定库的完整路径或使用-l(name of the library)但我仍然面对相同的消息

My makefile is 我的makefile是

$(CC) -O3 -o exe channel_mpi.o initialization.o convol_trasp.o fft_support.o data_man.o dnsdata.o dnsdirect.o /home/mirco/Scrivania/fftmpi-1Oct18/src/libfft3dmpi.a -lm 

The compiler is h5pcc, which is mpicc wrapped with some flags to use HDF5 library. 编译器是h5pcc,它是用一些标志包装的mpicc来使用HDF5库。

Have I done mistakes?? 我做错了吗?

Thanks for your help 谢谢你的帮助

operator new is a C++ thing. operator new是一个C ++的东西。 It seems you're building with C++ code and not C. The makefile variable $(CC) is the C compiler, $(CXX) is the C++ compiler. 看来你正在使用C ++代码构建而不是C. makefile变量$(CC)是C编译器, $(CXX)是C ++编译器。

So the solution is to switch the compiler you're using for linking. 所以解决方案是切换你用于链接的编译器。


When linking the C++ frontend program (typically g++ on Linux systems) will automatically add the C++ standard library. 链接C ++前端程序(通常是Linux系统上的g++ )时会自动添加C ++标准库。 It's this library that contains the default implementations of many C++ standard functions, like the functions needed for the C++ new operator. 这个库包含许多C ++标准函数的默认实现,比如C ++ new运算符所需的函数。

It is possible to use the C frontend ( gcc ) as well, but then you have to explicitly link with the C++ library. 也可以使用C前端( gcc ),但是你必须明确地链接到C ++库。 For gcc that means you have to add -lstdc++ . 对于gcc ,这意味着你必须添加-lstdc++

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM