gpt4 book ai didi

c++ - 如何使用 Code::Blocks 链接 C++ 源文件

转载 作者:行者123 更新时间:2023-11-30 01:27:05 25 4
gpt4 key购买 nike

我做错了什么,我知道。我不知道该怎么做通过头文件将两个 .cpp 文件链接在一起。召唤方法看不到其他来源。

我正在使用 Code::Blocks 作为 MinGW 的 IDE。

如有任何帮助,我们将不胜感激。还会更如果您能显示固定来源,请在回复中链接至带有它的 pastebin 页面。

/***********************************main.cpp***********************************/
#include <iostream>

using namespace std;

#include "test.h"

int main()
{
printTest(); //can't see printTest, defined in test.cpp
return 0;
};


/***********************************test.h***********************************/
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED

void printTest();

#endif // TEST_H_INCLUDED


/***********************************test.cpp***********************************/
#include "test.h"

void printTest()
{
cout << "Hello world!" << endl;
};

最佳答案

您可能会找到 this code blocks wiki有帮助。看起来代码块使用托管构建系统,因此如果您将文件正确添加到项目,那么它应该知道编译它并链接到生成的目标文件中。

当您使用“using namespace std;”时,只是为了更明确地说明其他一些注释。命名空间仅被引入 using 语句所在文件的范围。这就是其他人告诉您明确指定 std::命名空间的原因。您还可以将所有 std 命名空间纳入 test.cpp 文件的范围。许多人认为这是一个不好养成的习惯。通常最好通过以下方式将您需要的内容纳入范围

using std::cout;
using std::endl;

最后,请记住 std::endl 添加新行并刷新缓冲区,在所有情况下它都不是换行符的好替代品。

关于c++ - 如何使用 Code::Blocks 链接 C++ 源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9404626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com