如何在Visual Studio Code中包含编译器标志?

15

我有一个程序,正在尝试在Visual Studio Code调试器中运行使用fftw函数的程序。它可以通过以下命令编译:

g++ dimer.cpp -std=c++11 -lfftw3 

在我的计算机终端上编译程序时没有抱怨未定义的引用。但是,在生成了launch.json文件后,我的程序会抱怨fftw库函数和 -std=c++14 编译器标志。

我认为只需要额外使用-std=c++11-lfftw3 标志,以使Visual Studio Code中的调试器正常工作。我正在使用Microsoft的C/C++扩展和Code Runner扩展。

我正在尝试将一个Mathematica代码文档转换成c++。

以下是输出中显示的错误信息:

Executing task: /usr/bin/g++ -g /home/msammartino/Documents/twochain/dimer.cpp -o /home/msammartino/Documents/twochain/dimer <

In file included from /usr/include/armadillo:54:0,
             from /home/msammartino/Documents/twochain/dimer.cpp:6:
/usr/include/armadillo_bits/compiler_setup.hpp:530:108: note: #pragma message: NOTE: suggest to enable C++14 mode for faster code; add -std=c++14 to compiler flags
 #pragma message ("NOTE: suggest to enable C++14 mode for faster code; add -std=c++14 to compiler flags")
                                                                                                        ^
/tmp/ccgb7Xsv.o: In function `r2r_dsine_fftw_forward_dimer(int, double*, double*, Eigen::Matrix<double, 2, 2, 0, 2, 2> (&) [2048], Eigen::Matrix<double, 2, 2, 0, 2, 2> (&) [2048])':
/home/msammartino/Documents/twochain/dimer.cpp:99: undefined reference to `fftw_plan_r2r_1d'
/home/msammartino/Documents/twochain/dimer.cpp:100: undefined reference to `fftw_plan_r2r_1d'
/home/msammartino/Documents/twochain/dimer.cpp:101: undefined reference to `fftw_plan_r2r_1d'
/home/msammartino/Documents/twochain/dimer.cpp:102: undefined reference to `fftw_plan_r2r_1d'
/home/msammartino/Documents/twochain/dimer.cpp:103: undefined reference to `fftw_execute'
/home/msammartino/Documents/twochain/dimer.cpp:104: undefined reference to `fftw_execute'
/home/msammartino/Documents/twochain/dimer.cpp:105: undefined reference to `fftw_execute'
/home/msammartino/Documents/twochain/dimer.cpp:106: undefined reference to `fftw_execute'
/tmp/ccgb7Xsv.o: In function `r2r_dsine_fftw_backward_dimer(int, double*, double*, Eigen::Matrix<double, 2, 2, 0, 2, 2> (&) [2048], Eigen::Matrix<double, 2, 2, 0, 2, 2> (&) [2048])':
/home/msammartino/Documents/twochain/dimer.cpp:166: undefined reference to `fftw_plan_r2r_1d'
/home/msammartino/Documents/twochain/dimer.cpp:167: undefined reference to `fftw_plan_r2r_1d'
/home/msammartino/Documents/twochain/dimer.cpp:168: undefined reference to `fftw_plan_r2r_1d'
/home/msammartino/Documents/twochain/dimer.cpp:169: undefined reference to `fftw_plan_r2r_1d'
/home/msammartino/Documents/twochain/dimer.cpp:170: undefined reference to `fftw_execute'
/home/msammartino/Documents/twochain/dimer.cpp:171: undefined reference to `fftw_execute'
/home/msammartino/Documents/twochain/dimer.cpp:172: undefined reference to `fftw_execute'
/home/msammartino/Documents/twochain/dimer.cpp:173: undefined reference to `fftw_execute'
collect2: error: ld returned 1 exit status
The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.
请告诉我,如果我的问题表述有任何问题,请指出来。

1个回答

22
简单的方法是将它们作为args传递到您的tasks.json配置中
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build-all",
      "type": "shell",
      "args": [
          "-std=c++11",
          "-lfftw3",
          "-L",
          "/path/to/libs",
          "/path/to/file.cpp"
      ],
      "command": "g++",
    }
  ]
}

更易于维护和共享的选项是创建一个Makefile并在其中设置它们:
# Specify compiler to be used
CXX = g++
CXXFLAGS += -g -std=c++11 -fPIC -march=x86-64

# Specify paths to headers
INCLUDES += -I include

# Specify paths to the libraries
LDFLAGS  += -L /path/to/libs

# Specify the link libraries
LLIBS    += -lfftw3

# ... add other configs ...

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(OBJ_DIR)
    $(CXX) -c $(CXXFLAGS) $(INCLUDES) $< -o $@

$(OBJ_DIR)/$(PROGRAM): $(OBJS)
    $(CXX) $(LDFLAGS) $^ $(LLIBS) -o $@

然后在您的任务配置中,只需调用make

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build-all",
      "type": "shell",
      "options": {
          "cwd": "${workspaceFolder}",
          "env": {
            ...
          }
      },
      "command": "make -f Makefile.x86_64",
    }
  ]
}

如果您有依赖环境的路径,可以在Makefile中指定一个变量(例如MY_LIBS),然后在任务配置的env块中设置它们(例如"MY_LIBS": "/path/to/libs")。
Makefile选项的优点是:
  • 不使用VS Code的人仍然可以编译您的代码(从控制台或另一个IDE)。
  • 如果您正在使用CI / CD管道,则不需要单独的配置。您可以使用相同的Makefile在本地与VS Code一起构建并在CI / CD中构建。
  • 您可以将Makefile提交到存储库中,然后只需在本地的tasks.json配置中使用环境变量来指定特定于环境的设置。

2
我发现这个链接对理解 Make 文件很有用。http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/ - MattSamm
我不确定如何打开我的tasks.json文件,但它位于VSC文件目录中的隐藏“.vscode”文件中。当我尝试运行调试器时,会出现权限错误。我需要包括sudo吗?如果需要,您知道如何做吗?我该如何在这个问题中附上我的tasks.json文件? - MattSamm
这是 tasks.json 文件的预期位置,在您当前的 VSCode 工作区中的 .vscode 文件夹中。我觉得调试器需要 sudo 有点奇怪...看起来某些文件、二进制文件或者可能是 VSCode 本身是以 root 用户安装的,您没有权限访问这些文件。您尝试过在 VS Code 之外编译代码吗?从控制台? - Gino Mempin
我的代码在终端上不使用sudo也能编译通过。我使用了"g++ dimer.cpp -std=c++14 -lfftw3"。当我调用"./a.out"时,它似乎会无限运行/无法完成运行。这会对调试器造成问题吗? - MattSamm
你不应该需要使用 sudo 来编译和运行,所以这应该没问题。至于“无限运行”的问题,那现在就是你的应用程序的问题了。如果它在终端上无限运行,那么调试器也会表现出同样的行为,在 VS Code 调试器中也会无限运行。 - Gino Mempin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接