Error " undefined reference to `std::ios_base" while linking cpp header only library to fortran

huangapple go评论106阅读模式
英文:

Error " undefined reference to `std::ios_base" while linking cpp header only library to fortran

问题

我正在尝试将一个仅包含头文件的C++库与Fortran代码连接起来。我正在使用此示例来测试我的库。

  1. $ cat cppfunction.C
  2. #include <cmath>
  3. #include <mylib/mylib.hpp>
  4. extern "C"
  5. {
  6. void cppfunction_(float *a, float *b);
  7. }
  8. void cppfunction_(float *a, float *b)
  9. {
  10. *a = 7.0;
  11. *b = 9.0;
  12. }
  13. $ cat fprogram.f
  14. program fprogram
  15. real a, b
  16. a = 1.0
  17. b = 2.0
  18. print*, "Before fortran function is called"
  19. print*, 'a=', a
  20. print*, 'b=', b
  21. call cppfunction(a, b)
  22. print*, "After cpp function is called"
  23. print*, 'a=', a
  24. print*, 'b=', b
  25. stop
  26. end

编译时我使用了以下命令:

  1. $ gfortran -c fprogram.f
  2. $ g++ -c cppfunction.C
  3. $ gfortran -lc -o fprogram fprogram.o cppfunction.o

如果我移除我的库头文件,这个编译是正常的。但是当我包括它时,会出现以下错误:

  1. cppfunction.o: In function `__static_initialization_and_destruction_0(int, int)':
  2. cppfunction.C:(.text+0xa1): undefined reference to `std::ios_base::Init::Init()'
  3. cppfunction.C:(.text+0xb0): undefined reference to `std::ios_base::Init::~Init()'
  4. collect2: error: ld returned 1 exit status

我可能做错了什么吗?

  1. <details>
  2. <summary>英文:</summary>
  3. I am trying to link a header only library (which is in cpp) to a fortran code. I am using [this example][1] to test my library.
  4. $ cat cppfunction.C
  5. #include&lt;cmath&gt;
  6. #include&lt;mylib/mylib.hpp&gt;
  7. extern &quot;C&quot;
  8. {
  9. void cppfunction_(float *a, float *b);
  10. }
  11. void cppfunction_(float *a, float *b)
  12. {
  13. *a=7.0;
  14. *b=9.0;
  15. }
  16. $ cat fprogram.f
  17. program fprogram
  18. real a,b
  19. a=1.0
  20. b=2.0
  21. print*,&quot;Before fortran function is called&quot;
  22. print*,&#39;a=&#39;,a
  23. print*,&#39;b=&#39;,b
  24. call cppfunction(a,b)
  25. print*,&quot;After cpp function is called&quot;
  26. print*,&#39;a=&#39;,a
  27. print*,&#39;b=&#39;,b
  28. stop
  29. end
  30. For compiling I am using:
  31. $ gfortran -c fprogram.f
  32. $ g++ -c cppfunction.C
  33. $ gfortran -lc -o fprogram fprogram.o cppfunction.o
  34. This runs fine if I remove my library header. But have this error when included:
  35. cppfunction.o: In function `__static_initialization_and_destruction_0(int, int)&#39;:
  36. cppfunction.C:(.text+0xa1): undefined reference to `std::ios_base::Init::Init()&#39;
  37. cppfunction.C:(.text+0xb0): undefined reference to `std::ios_base::Init::~Init()&#39;
  38. collect2: error: ld returned 1 exit status
  39. Anything I might be doing wrong?
  40. [1]: https://www.cae.tntech.edu/help/programming/mixed_languages
  41. </details>
  42. # 答案1
  43. **得分**: 0
  44. 你没有链接C++标准库:
  45. gfortran -lc -lstdc++ -o fprogram fprogram.o cppfunction.o
  46. // ^^^^^^^^
  47. <details>
  48. <summary>英文:</summary>
  49. You&#39;re not linking the C++ standard library:
  50. gfortran -lc -lstdc++ -o fprogram fprogram.o cppfunction.o
  51. // ^^^^^^^^
  52. </details>

huangapple
  • 本文由 发表于 2020年1月6日 20:28:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定