code::blocks,如何将链接器选项`-lstdc++`放在编译命令的末尾?
code::blocks, how to put the linker option `-lstdc++` at the end of the compiler command?
IDE 是 code::blocks ubuntu 18.04。
工作区包含两个简单的项目。第一个是一个 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
。似乎一切都OK。
另一个项目有一个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::blocks中设置它。
如果有人可以提供任何建议,我将不胜感激。
通过设置settings->compiler->other settings->advanced options
解决,改变命令行宏命令。然后将链接器选项 -lstdc++
放在命令的末尾。问题消失。
IDE 是 code::blocks ubuntu 18.04。 工作区包含两个简单的项目。第一个是一个 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
。似乎一切都OK。
另一个项目有一个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::blocks中设置它。 如果有人可以提供任何建议,我将不胜感激。
通过设置settings->compiler->other settings->advanced options
解决,改变命令行宏命令。然后将链接器选项 -lstdc++
放在命令的末尾。问题消失。