Code::块,如何将链接器选项`-lstdc++`放在编译器命令的末尾?

2022-04-10 00:00:00 fortran c++ codeblocks mixed

该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++放在命令的末尾。问题就消失了。

相关文章