我在我的Windows电脑上测试一个程序时遇到了这个问题,并尽可能简化代码以尝试找出问题所在
我有
main.cxx
:
#include <iostream>
#include "convert.h"
int main() {
std::cout << "hi" << std::endl;
}
convert.h
:
#include <string>
std::string convert (std::string);
和
convert.cxx
:
#include <string>
#include "convert.h"
std::string convert (std::string str) {
return str;
}
我用 MinGW gcc 9.2 版编译
> g++ main.cxx convert.cxx
但是当运行输出时
> ./a.exe
什么都没发生。
无论出于何种原因,当我注释掉
convert.cxx
中的函数定义时:
#include <string>
#include "convert.h"
//string convert (string str) {
// return str;
//}
重复这些步骤确实给出了预期的输出。
我也尝试删除不同的包含语句,
using namespace std
在不同的文件中,但唯一不同的是我是否在convert.cxx
中定义了一个函数。
我错过了什么?
这是我对 SO 的第一条评论,如果我的语法或措辞晦涩难懂,请原谅我。
首先,我建议您的包含文件需要一个标题保护,这可以通过
#ifndef
/ #define
完成,在文件末尾您可以使用 #endif
来防止多次包含说文件。
接下来,如其他评论所述,我强烈建议您删除当前使用的 MinGW,并获取最新版本。它与 VSC 一起更加稳定和可行。