小编anu*_*ria的帖子

在linux中用自己的头文件编译和运行c++程序

这是我第一次制作自己的头文件。我正在尝试在 Ubuntu 上用 C++ 制作一个简单的 Hello World 程序。制作了 3 个文件,如下所示:

//你好.h文件

#ifndef HELLO_H
#define HELLO_H

//my own code

void hello();

#endif
Run Code Online (Sandbox Code Playgroud)

//你好.cpp文件

#include <iostream>
#include "hello.h"

using namespace std;

void hello()
{
    cout << "This line is printed from header.";
}
Run Code Online (Sandbox Code Playgroud)

//main.cpp文件

#include <iostream>
#include "hello.h"

using namespace std;

int main()
{
    cout << "in main" << endl << endl;
    hello();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我试过了

g++ -I /home/Desktop/hello/ -c hello.cpp -o hello.o
Run Code Online (Sandbox Code Playgroud)

编译头文件,此命令有效。

然后,在做的时候

g++ -o main main.cpp
Run Code Online (Sandbox Code Playgroud)

我结束了以下错误:

/tmp/ccb0DwHP.o: …
Run Code Online (Sandbox Code Playgroud)

c++ linux header-files

0
推荐指数
1
解决办法
3万
查看次数

标签 统计

c++ ×1

header-files ×1

linux ×1