要尝试C++中的C代码缠绕问题,我使用了以下内容: header.h与包裹一个C++代码
#ifdef __cplusplus
extern "C"
#endif
void func();
source.cpp
#include "header.h"
#include <iostream>
extern "C" void func()
{
std::cout << "This is C++ code!" << std::endl;
}
和由source.c
#include "header.h"
int main()
{
func();
}
为了编译和链接,我用了下面的顺序:
g++ -c source.cpp
gcc source.c source.o -o myprog
我得到的错误是: ENCE到std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' source.cpp:(.text+0x1c): undefined reference to
的std :: basic_ostream> ::操作< <(STD :: basic_ostream> &(*)(STD :: basic_ostream> &))” source.o :在功能__static_initialization_and_destruction_0(int, int)': source.cpp:(.text+0x45): undefined reference to
的std ::的ios_base :: ::初始化的init()” source.cpp :(文字+ 0x4a):未定义的引用std::ios_base::Init::~Init()' source.o:(.eh_frame+0x12): undefined reference to
__gxx_personality_v0' collect2:LD返回1退出状态
我怎么能让这个简单的代码编译并运行?它应该作为我未来 开发的基础。
谢谢,第一个选项工作:)。如何在一行中执行上述选项1? – user506901
'g ++ -o myprog source.cpp source.c' – trojanfoe
这不会将c源代码编译为C++吗? – Will03uk