如何在Visual Studio Code调试器中包括编译器标志?

问题描述 投票:2回答:1

我有一个试图在使用fftw函数的Visual Studio Code调试器中运行的程序。用以下命令编译]

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.

请让我知道我问这个问题的方式有什么问题。

c++ visual-studio-code fftw
1个回答
3
投票

最简单的选择是在任务配置中将它们作为args传递:

{
    "label": "build-all",
    "type": "shell",
    "args": [
        "-std=c++11",
        "-lfftw3",
        "-L",
        "/path/to/libs",
        "/path/to/file.cpp"
    ],
    "command": "g++",
},

更易于维护,可共​​享的选项是create a 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

...

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

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

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

{
    "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平台/引擎,则不需要单独的配置
  • 您可以将Makefile提交到存储库,然后仅在本地task.json配置中使用环境变量来指定特定于环境的设置
© www.soinside.com 2019 - 2024. All rights reserved.