【问题标题】:CMake & Visual Studio project setupCMake 和 Visual Studio 项目设置
【发布时间】:2019-05-08 12:14:06
【问题描述】:

我正在使用 Visual Studio 2019 IDE 用 c++ 编写自己的 minecraft 启动器。我希望它是一个跨平台的项目。我决定为此使用 CMake,但是我在使用第三方库时遇到了一些问题。

文件结构

root
|---MyProject
|   |---build
|   |   |---Debug
|   |   |---Release
|   |---include
|   |   |---MyProject.hpp
|   |---src
|   |   |---MyProject.cpp
|   |   |---CMakeLists.txt
|   |---CMakeLists.txt
|---Vendor
|   |---CURL
|   |   |--- // source downloaded from https://curl.haxx.se/download/curl-7.64.1.tar.gz
|   |---CMakeLists.txt
|---CMakeLists.txt

我有一些在 Visual Studio 的解决方案中链接库的经验,但我不知道如何在 CMake 中进行。

我有两个文件夹:

  1. 我的项目(我正在处理)文件夹,其中包含所有 .cpp 和 .hpp 文件。
  2. 所有第三方库的供应商文件夹。

我想将“供应商”中的 CMake 项目链接到“MyProject”,以便能够在“MyProject.cpp”中使用并构建它。

示例用法:

'MyProject.hpp':

#pragma once
#include <iostream>
#inlcude "curl/curl.h"

'MyProject.cpp':

int main() {
    // Hello World
    std::cout << "Hello world" << std::endl;
    // Some Curl stuff
    CURL* curl;
    ...
}

我尝试过这样的事情:

add_subdirectory("Vendor/CURL")
include_directories("Vendor/CURL/include")

我是 CMake 新手,不知道该怎么做... 我在谷歌上搜索了一个多小时,但我没有找到任何东西。 顺便说一句:对不起我的英语。

【问题讨论】:

  • 您是否使用 target_link_libraries() 将供应商项目链接到 MyProject?
  • 而且,虽然这不是您的问题的一部分,但我想指出不应使用 include_directories()。如果在 CMake 中正确设置了 CURL(我不知道,我从未使用过它),那么 target_link_libraries() 应该处理包含目录。 Imo,这是一个关于 CMake 的精彩演讲,以及为什么你不应该使用 include_directories():youtube.com/watch?v=bsXLMQ6WgIk
  • @Lehks 好的,谢谢,我会看看。

标签: c++ visual-studio cmake


【解决方案1】:

要链接到第三方库,您需要指定库的路径和名称。

我有一些在 Visual Studio 中链接库的经验 解决方案,但我不知道如何在 CMake 中做到这一点。

CMake link_directories 映射到 VS Properties-&gt;Linker-&gt;General-&gt;Additional Library Directories

CMake target_link_libraries 映射到 VS Properties-&gt;Linker-&gt;Input-&gt;Additional Dependencies

我是 CMake 新手,不知道该怎么做。

看看CMake and Visual Studio,它通过一个示例在 Visual Studio 的上下文中解释了 CMake。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多