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

Mat*_*amm 8 c++ fftw visual-studio-code

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

g++ dimer.cpp -std=c++11 -lfftw3 
Run Code Online (Sandbox Code Playgroud)

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

我认为,它需要的只是额外的标志-std=c++11,并-lfftw3 在Visual Studio代码调试工作。我正在使用 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.
Run Code Online (Sandbox Code Playgroud)

请让我知道我提出这个问题的方式有任何问题。

Gin*_*pin 15

简单的选择是在你的 tasks.json 配置中传递它们args

{
  "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++",
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

更易于维护、可共享的选项是创建一个 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 $@
Run Code Online (Sandbox Code Playgroud)

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

# 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 $@
Run Code Online (Sandbox Code Playgroud)

如果您有依赖于 env 的路径,您可以在 Makefile 中指定一个变量(例如MY_LIBS),然后env在任务配置的块中设置它们(例如"MY_LIBS": "/path/to/libs")。

Makefile 选项的优点是:

  • 不使用 VS Code 的人仍然可以编译您的代码(从控制台或其他 IDE)。
  • 如果您使用的是 CI/CD 管道,则不需要单独的配置。您可以使用相同的 Makefile 使用 VS Code 在本地构建以及使用 CI/CD 构建。
  • 您可以将 Makefile 提交到存储库,然后只需在本地 tasks.json 配置中使用环境变量来指定特定于 env 的设置。

  • 我发现此链接对于理解 Make 文件是什么很有用。http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/ (2认同)