【问题标题】:Visual Studio C++ remote linux debugging add linker optionsVisual Studio C++ 远程 linux 调试添加链接器选项
【发布时间】:2018-03-22 08:01:39
【问题描述】:

我正在尝试使用 boost 库在 C++ 中开发一个简单的程序。 我使用 Visual Studio 2017 和 ubuntu 的远程 bash shell 进行编译和调试。

我在ubuntu上安装了gdb、gdbserver、所有编译器和boost库。

没有 boost 的简单程序可以直接从 shell 编译和运行,就像从 Visual Studio 一样!

当我使用以下命令直接从 ubuntu bash 编译以下程序时:g++ test.cpp -std=c++11 -lboost_program_options -o t 它也会编译并运行!

#include <boost/program_options.hpp>
#include <iostream>

using namespace boost::program_options;

int main(int argc, const char *argv[])
{
    try
    {
        options_description desc{ "Options" };
        desc.add_options()
            ("help,h", "Help screen");

        variables_map vm;
        store(parse_command_line(argc, argv, desc), vm);
        notify(vm);

        if (vm.count("help"))
            std::cout << desc << '\n';

    }
    catch (const error &ex)
    {
        std::cerr << ex.what() << '\n';
    }
}

但是如果我将相同的代码放在 Visual Studio 中的一个文件中并尝试远程编译它就不起作用:

1>------ Build started: Project: ACO-PPS, Configuration: Debug x64 ------
1>Validating architecture
1>Validating sources
1>Copying sources remotely to 'localhost'
1>Starting remote build
1>Compiling sources:
1>main.cpp
1>Linking objects
1>/home/marius/projects/ACO-PPS/obj/x64/Debug/main.o : In the function main :
1>/home/marius/projects/ACO-PPS/main.cpp:11 : undefined reference to « boost::program_options::options_description::m_default_line_length »

等等……

在项目属性中,我将-lboost_program_options 包含在: 配置属性 > C/C++ > 所有选项 > 附加选项 和下: 配置属性 > 链接器 > 所有选项 > 附加选项

我做错了什么?

【问题讨论】:

    标签: visual-studio ubuntu boost remote-debugging boost-program-options


    【解决方案1】:

    以下是 VCLinux 用于为 GCC 指定库的规则 - 来自 Ion Todirel (MSFT) 在 answer on the VCLinux GitHub site 中。

    您会看到...Additional Options 将库放在 目标文件之前,因此链接器不会在库中查找依赖项。我建议使用 Linker - Input - Library Dependencies 并指定库名称 boost_program_options 不带 -l

    • 链接器 - 常规 - 附加库目录 - 这会添加路径 在命令开头附近的链接器命令行中使用 -L 行。

    • 链接器 - 输入 - 库依赖项 - 这会添加文件名 -l 在链接器命令行的最末尾

    • 链接器 - 输入 - 附加依赖项 - 这会添加条目 在目标文件之后和链接器之前逐字逐句 - 输入 - 库依赖

    • 链接器 - 命令行 - 附加选项 - 这会添加条目 在链接器命令行中的目标文件之前逐字逐句

    注意Linker - Input - Library Dependencies 中给出的库名称作为-l 命令行选项传递给gcc;即它不应该有 lib 前缀或扩展名。例如,libcairo.so 应在 Linker - Input - Library Dependencies 中显示为 cairo。在 Linux 远程,gcc 将沿着Linker - General - Additional Library Directories 中指定的路径和默认系统库搜索路径搜索,首先查找libcairo.so(动态链接或共享库)然后libcairo.a(静态链接图书馆)。

    如果您的系统上同时具有共享库和静态库,则将优先使用共享库。如果您想强制链接静态库,请参阅Telling gcc directly to link a library statically

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多