使用 VSCode Code Runner 为 C++ 编译头文件 [英] Compiling header files for C++ using VSCode Code Runner

查看:318
本文介绍了使用 VSCode Code Runner 为 C++ 编译头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VSCode 和代码运行器扩展来尝试使用包含文件运行一个简单的 C++ 项目.

I am using VSCode and the code runner extension to try to run a simple c++ project with include files.

项目由一个 main.cpp 组成:

The project is made of a main.cpp:

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

using namespace std;

int main(void)
{


    putHello();

    return 0;
}

一个 hello.hpp 头文件:

A hello.hpp header file:

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

#endif

还有 hello.cpp:

And the hello.cpp:

#include <iostream>

using namespace std;

void putHello(void){
  cout<<"Hello"<<endl;
}

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

If I define putHello() in main.cpp, the code compiles. If I include #include cval.cpp the code compiles.

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

However, when I only include the cval.hpp header and use the Code runner extension, I get the following error:

  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 code 1 (use -v to see invocation)

我知道这是我没有编译 cval.cpp 的问题,因此链接器无法找到它并正确链接它.但是,有没有办法让 VSCode 自动编译头文件中包含的文件?

I understand this is a problem that I am not compiling cval.cpp so the linker cannot find it and link it properly. However, is there a way to get VSCode to automatically compile files that were included in headers?

我不想一直指定它或必须包含 .cpp 文件,它能够在我的 C 代码项目中包含这些文件而不会出现问题.

I don't want to have to specify it all the time or have to include the .cpp files, and it is able to include these files with out a problem in my C code projects.

推荐答案

解决方法有两种.

在设置(cmd + ,)中,找到运行代码配置.

In settings(cmd + ,), find Run Code Configuration.

有一个选项卡执行器映射"并打开在 settings.json 中编辑".

There is a tab 'Executor map' and open 'edit in settings.json'.

然后就可以看到code-runner扩展(executor map)的设置了.

Then you can see the settings for code-runner extension (executor map).

将cpp部分改成这个

"cpp": "cd $dir && g++ -o fileNameWithoutExt *.cpp && ./$fileNameWithoutExt"

此代码编译当前目录中的每个 cpp 文件,因此将正确包含标头.

This code compiles every cpp file in the current directory, so the header will be included properly.

这个解决方案的一个问题是,如果有其他带有main()的cpp文件,那么它就不会被正确编译.

A problem of this solution is, if there are other cpp files with main(), then it will not be compiled properly.

所以你最好为一个 cpp 程序使用一个文件夹.

So you'd better use one folder for one cpp program.

我做了第二个解决方案来避免这个问题,但它只适用于 mac(因为我在 bash 配置文件上创建了一个函数,而且我对 powershell 脚本语法不够熟悉,无法在 windows 上创建该函数)

I made a second solution to avoid this problem, but it only works for mac (because I made a function on the bash profile, and I'm not familiar enough with powershell script syntax to make that function on windows)

如果你使用 macOS,试试这个.

If you use macOS, try this one.

在 vscode 上,按 (shift + cmd + p) 并输入

On vscode, press (shift + cmd + p) and type

Shell Command: Install 'code' command in PATH

然后打开终端,输入

sudo code ~/.bash_profile

如果您使用 zsh,那么请输入

if you use zsh, then instead of that, type

sudo code ~/.zshrc

并复制粘贴此代码

function cppcompile()
{
  filename=$1
  re="^\#include \""
  while read line
  do
    if [[ $line =~ $re ]]; then
      temp=${line:9}
      temp1=${temp#\"}
      temp2=${temp1%\.*\"}
      g++ -std=c++11 -c $temp2.cpp
    fi
  done < $filename.cpp
  g++ -std=c++11 -c $filename.cpp
  g++ -o $filename *.o
  ./$filename
  rm *.o
}

然后再次打开executor map,将cpp部分改成

and then open the executor map again, and change the cpp part into

"cpp": "cd $dir && cppcompile $fileNameWithoutExt"

cppcompile 函数会读取你的代码并找到头文件,并编译头文件和主代码.

cppcompile function will read your code and find headers, and compile headers and main code.

所以它可以编译唯一的一个main代码和链接到那个main的头文件,并且同目录下是否有其他带有main的cpp文件都没有关系.

So it can compile the only one main code and the headers linked to that main, and it doesn't matter if there are other cpp files with main in the same directory.

这篇关于使用 VSCode Code Runner 为 C++ 编译头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆