Search code examples
c++fortrancodeblocksmixed

code::blocks, how to put the linker option `-lstdc++` at the end of the compiler command?


The IDE is code::blocks on ubuntu 18.04. The workspace contains two simple projects. The first one is a c++ static library with only one cpp file presented in follows.

// 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 log is:

-------------- 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))

The file libtry.a is generated. It seems everything is OK. The other project has a fortran program as follows:

! 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

The c++ lib libtry.a is linked to this fortran project by setting build options-->linker settings-->link libraries. Errors appears when compiling the fortran project:

-------------- 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))

The problem is more clear now. It can be solved by changing

gfortran  -o bin/Debug/try2 obj/Debug/main.o  -lstdc++  ../try/obj/Debug/try.o

to

gfortran  -o bin/Debug/try2 obj/Debug/main.o  ../try/obj/Debug/try.o -lstdc++ 

. But I don't know how to set it in code::blocks. I appreciate it if anyone can provide any suggestions.


Solution

  • It is solved by setting the settings->compiler->other settings->advanced options, change the orders of the command line macro. Then the linker option -lstdc++ is put to the end of the command. The problem disappear.