code::blocks, how to put the linker option `-lstdc++` at the end of the compiler command?(Code::块,如何将链接器选项`-lstdc++`放在编译器命令的末尾?)
问题描述
该IDE是ubuntu 18.04上的code::BLOCKS。 工作区包含两个简单的项目。第一个是一个c++静态库,其中只有一个CPP文件,如下所示。
// try.cpp -- The c++ static library is defined as:
# include <iostream>
extern "C"
{
void shownum ( int n );
}
void shownum ( int n ){
using namespace std;
std::cout<<n<<endl;
}
生成日志为:
-------------- Build: Debug in try (compiler: GNU GCC Compiler)---------------
g++ -Wall -fexceptions -c /home/try/try.cpp -o obj/Debug/try.o
rm -f bin/Debug/libtry.a
ar -r -s bin/Debug/libtry.a obj/Debug/try.o
ar: creating bin/Debug/libtry.a
Output file is bin/Debug/libtry.a with size 2.78 KB
Process terminated with status 0 (0 minute(s), 1 second(s))
0 error(s), 0 warning(s) (0 minute(s), 1 second(s))
文件libtry.a
生成。看起来一切都很好。
另一个项目的Fortran程序如下:
! main.f90 -- The main program by fortran is:
program main
implicit none
integer*4 y
interface
subroutine shownum(np) bind(C)
use, intrinsic :: iso_c_binding
implicit none
integer (c_int), value :: np
end subroutine shownum
end interface
y=5
call shownum(y)
end
c++库libtry.a
通过设置build options-->linker settings-->link libraries
链接到此Fortran项目。编译Fortran项目时出现错误:
-------------- Build: Debug in try2 (compiler: GNU Fortran Compiler)---------------
gfortran -Jobj/Debug/ -Wall -g -c /home/try2/main.f95 -o obj/Debug/main.o
gfortran -o bin/Debug/try2 obj/Debug/main.o ../try/bin/Debug/libtry.a
../try/bin/Debug/libtry.a(try.o): In function `shownum':
try.cpp:(.text+0x13): undefined reference to `std::cout'
try.cpp:(.text+0x18): undefined reference to `std::ostream::operator<<(int)'
try.cpp:(.text+0x22): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
try.cpp:(.text+0x2d): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
../try/bin/Debug/libtry.a(try.o): In function `__static_initialization_and_destruction_0(int, int)':
try.cpp:(.text+0x59): undefined reference to `std::ios_base::Init::Init()'
try.cpp:(.text+0x6e): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
6 error(s), 0 warning(s) (0 minute(s), 1 second(s))
问题现在更清楚了。 可以通过更改
来解决gfortran -o bin/Debug/try2 obj/Debug/main.o -lstdc++ ../try/obj/Debug/try.o
至
gfortran -o bin/Debug/try2 obj/Debug/main.o ../try/obj/Debug/try.o -lstdc++
。但我不知道如何在code::块中设置它。 如果有人能提供任何建议,我将不胜感激。
推荐答案
通过设置settings->compiler->other settings->advanced options
,更改命令行宏的顺序来解决。然后将链接器选项-lstdc++
放在命令的末尾。问题就消失了。
这篇关于Code::块,如何将链接器选项`-lstdc++`放在编译器命令的末尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Code::块,如何将链接器选项`-lstdc++`放在编译器命令的末尾?


- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- GDB 不显示函数名 2022-01-01
- DoEvents 等效于 C++? 2021-01-01
- OpenGL 对象的 RAII 包装器 2021-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- 如何提取 __VA_ARGS__? 2022-01-01
- 将函数的返回值分配给引用 C++? 2022-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- 将 hdc 内容复制到位图 2022-09-04
- XML Schema 到 C++ 类 2022-01-01