【问题标题】:VSCode dynamic linking - C++ Boost TestVSCode 动态链接 - C++ Boost 测试
【发布时间】:2019-06-14 07:43:32
【问题描述】:

我正在尝试让 Boost 单元测试程序在 VSCode 中运行,但在链接库时遇到问题。

我编写了一个简单的单元测试设置,其中包含 Boost Test 和 FakeIt 模拟框架这两个类,并且很容易验证它可以在 VS2017 中运行。动机是让它最终在 VSCode 中工作。 当我构建项目时,它无法找到升压测试文件。我尝试了一些不同的包含选项;绝对路径、相对路径和环境变量(如下面的 c_cpp_properties.json 所示)。

tasks.json

{
"version": "2.0.0",
"tasks": [
    {
        "label": "echo",
        "type": "shell",
        "command": "g++ -g -Wall main.cpp -L/c:/boost/lib64-msvc-14.1 -llibboost_unit_test_framework-vc141-mt-gd-x64-1_69",
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]
}

launch.json

{
"version": "0.2.0",
"configurations": [
    {
        "name": "(gdb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/a.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "MIMode": "gdb",
        "miDebuggerPath": "C:/MinGW/bin/gdb.exe",
        "preLaunchTask": "echo",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    }
]
}

c_cpp_properties.json

{
"configurations": [
    {
        "name": "Win32",
        "includePath": [
            "${workspaceFolder}/**",
            "${env:BOOST_ROOT}",
            "${env:FAKEIT}"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "windowsSdkVersion": "10.0.17763.0",
        "compilerPath": "C:/MinGW/bin/gcc.exe",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "gcc-x64",
        "browse": {
            "path": [
                "${C:/MinGW/lib/gcc/mingw32/6.3.0/include/c++}"
            ]
        }
    }
],
"version": 4
}

我得到的是

Executing task: g++ -g main.cpp <

main.cpp:3:10: fatal error: boost/test/unit_test.hpp: No such file or directory
 #include <boost/test/unit_test.hpp>
          ^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
The terminal process terminated with exit code: 1

我是否错误地设置了构建文件?习惯了 VS2017,我对手动编写构建文件还很陌生,但我确实在 VSCode Cpp 中制作了一个有效的 Hello World 程序,所以它不像编译器不工作。希望有人可以提供帮助,如果需要,我可以提供 cpp/h 文件,但鉴于我知道它在 VS2017 中有效,这似乎没有必要。

Edit1:我什至可以右键单击 Boost 和 FakeIt 包含并“转到定义”,它会打开文件,但是当我尝试构建它时说它不能...?!

Edit2:在搜索makefile编译器命令后,我将命令更新为

g++ -g -Wall main.cpp -L/c:/boost/lib64-msvc-14.1 
-llibboost_unit_test_framework-vc141-mt-gd-x64-1_69

,试图告诉编译器链接库(.lib 和 .dll 文件都存在于该文件夹中)但它没有改变任何东西。

干杯, 超光速粒子

【问题讨论】:

  • BOOST_ROOT 定义为什么?
  • 这些是我的环境变量,BOOST_ROOT 是 C:\boost
  • 那么你有文件 C:\boost\boost/test/unit_test.hpp 吗?路径中两次提到 boost 的事实让我很困扰。
  • 是的,没错。这就是设置 boost 文件夹的方式。 C:\boost\boost\test\unit_test.hpp 是我需要的文件,所以我认为我写的方式是正确的。但它没有找到它......!
  • 好吧,我的第二个观察是你的编译器命令行似乎是g++ -g main.cpp。我不知道您正在使用的构建系统,但如果这就是命令行中的全部内容,那么这显然是错误的。我希望 -I 选项告诉编译器在哪里查找头文件,例如g++ -I C:\boost -g main.cpp

标签: c++ visual-studio-code boost boost-test


【解决方案1】:

我不知道你为什么认为这是一个链接问题,错误信息肯定是告诉你这是一个编译问题。让您知道这一点的消息部分肯定是:

编译终止。

如果这是一个链接问题,gcc 链接器会说“链接失败”。

这是编译问题的另一个线索是:

main.cpp:3:10: 致命错误:boost/test/unit_test.hpp: 没有这样的文件或目录

这是 c 预处理器告诉您它找不到源代码要求的文件。包含发生在编译时而不是链接时。

您的问题只是您忘记告诉编译器 boost 标头在该行的 tasks.json 中的位置:

“命令”:“g++ -g -Wall main.cpp -L/c:/boost/lib64-msvc-14.1 -llibboost_unit_test_framework-vc141-mt-gd-x64-1_69”,

-L 选项告诉链接器在哪里找到文件(而不是编译器)。它需要阅读:

“命令”:“g++ -g -Wall main.cpp -Ic:/boost -L/c:/boost/lib64-msvc-14.1 -llibboost_unit_test_framework-vc141-mt-gd-x64-1_69”,

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    相关资源
    最近更新 更多