小编eas*_*sox的帖子

使用 VSCode Code Runner 编译 C++ 头文件

我正在使用 VSCode 和代码运行程序扩展来尝试运行带有包含文件的简单 C++ 项目。

该项目由 main.cpp 组成:

#include <iostream>
#include <time.h>
#include "cval.hpp"

using namespace std;

int main(void)
{


    putHello();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

hello.hpp 头文件:

#ifndef CVAL_H
#define CVAL_H
void putHello(void);

#endif
Run Code Online (Sandbox Code Playgroud)

还有 hello.cpp:

#include <iostream>

using namespace std;

void putHello(void){
  cout<<"Hello"<<endl;
}
Run Code Online (Sandbox Code Playgroud)

如果我在 main.cpp 中定义putHello(),代码就会编译。如果我包含#include cval.cpp代码编译。

但是,当我仅包含cval.hpp标头并使用代码运行器扩展时,出现以下错误:

  Undefined symbols for architecture x86_64:
  "putHello()", referenced from:
      _main in main-a07db2.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit …
Run Code Online (Sandbox Code Playgroud)

c++ visual-studio-code

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

标签 统计

c++ ×1

visual-studio-code ×1