编译和链接调用c++函数的C代码
Compiling and linking C code invoking C++ function
当从我的C代码调用c++方法时,我有一个问题。我需要在c++代码中调用的方法不在类中。我试图设置一个简单的例子,我有以下文件:
//header.h
#ifdef __cplusplus
#include <iostream>
extern "C" {
#endif
int print(int i, double d);
#ifdef __cplusplus
}
#endif
//ccode.c
#include "header.h"
main() {
printf("hello");
print(2,2.3);
}
//cppcode.cc
#include "header.h"
using namespace std;
int print(int i, double d)
{
cout << "i = " << i << ", d = " << d;
}
可能我的错误是在我试图编译和链接这个方式。我正在做以下事情:
g++ -c cppcode.cc -o cppcode.o
很好。
gcc ccode.c cppcode.o -o ccode
在这里我得到了以下错误:
cppcode.o: In function `print':
cppcode.cc:(.text+0x16): undefined reference to `std::cout'
cppcode.cc:(.text+0x1b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
cppcode.cc:(.text+0x28): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
cppcode.cc:(.text+0x35): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
cppcode.cc:(.text+0x42): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)'
cppcode.o: In function `__static_initialization_and_destruction_0(int, int)':
cppcode.cc:(.text+0x6b): undefined reference to `std::ios_base::Init::Init()'
cppcode.cc:(.text+0x70): undefined reference to `std::ios_base::Init::~Init()'
collect2: ld returned 1 exit status
我认为这是因为我正在使用C编译器。编译和链接这个小示例的正确方法是什么?这个想法是,我有C代码运行,只是调用c++函数,而不必在C中重写它们。
我使用的是Ubuntu 12.04, gcc版本4.6.3
需要链接c++运行库。
gcc ccode.c cppcode.o -o ccode -lstdc++
应该分别编译和链接。使用g++
进行链接,以获得合适的标准库。
g++ -c cppcode.cc -o cppcode.o
gcc -c ccode.c -o ccode.o
g++ ccode.o cppcode.o -o ccode
g++编译器会自动将您的程序与标准cpp库链接起来。当您使用gcc进行编译时,链接器可以找到对它的引用。你有两个选择。一种是用c++编译c文件。第二种是强制链接标准cpp库。
下面是gcc指南中的内容:-static-libstdc + +
When the g++ program is used to link a C++ program, it normally automatically links against libstdc++. If libstdc++ is available as a shared library, and the -static option is not used, then this links against the shared version of libstdc++. That is normally fine. However, it is sometimes useful to freeze the version of libstdc++ used by the program without going all the way to a fully static link. The -static-libstdc++ option directs the g++ driver to link libstdc++ statically, without necessarily linking other libraries statically.
执行如下命令:
gcc ccode.c cppcode.o -o ccode -lstdc++
相关文章:
- 代码在main()中运行,但在函数中出现错误
- 为什么我在使用void函数时得到错误代码C2276
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 这个c++代码是如何在没有定义函数的情况下运行的
- 如何在没有函数的情况下编写此代码并使C++更简单?
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 了解每月第一天函数的代码
- 代码的效率. 转到和函数调用
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 如何定义一个没有重复代码的继承的 const 类成员函数?
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- 以下代码中的函数模板有什么问题?
- 为什么这些代码仍然使用 lvalue 函数?
- 在这个函数中是有缺陷的,因为取消引用 null 是无效的,所以我想更改代码
- 为什么下面带有非常量转换函数的代码没有歧义?
- C++代码停止工作错误使用cout内部函数
- 如何将 c++ get 函数代码转换为 opencv 算法中使用的 python
- 如何在c++中重用代码(函数)
- 编译来自学习现代 3D 图形编程的失败代码 函数___tmainCRTStartup中引用未解析的外部符号_main
- 混合代码-函数成员指针