CMake-更改lib和include目录用于外部项目和正确链接的位置 [英] Cmake - change where the lib and include directories are for an externalproject and proper linking

查看:89
本文介绍了CMake-更改lib和include目录用于外部项目和正确链接的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cmake_minimum_required(VERSION 3.2.1 FATAL_ERROR)
project("soxy")

add_definitions(-std=c99)

include_directories(soxy)

# Includes
include(ExternalProject)

ExternalProject_Add(
    LibUV
    GIT_REPOSITORY "https://github.com/libuv/libuv.git"
    GIT_TAG "v1.4.2"
    CONFIGURE_COMMAND sh <SOURCE_DIR>/autogen.sh && ./configure --prefix=${CMAKE_CURRENT_SOURCE_DIR}
    PATCH_COMMAND ""
    BUILD_COMMAND "make"
    BUILD_IN_SOURCE 1
    PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/build
)

set( SOXY_SRC src/file_utils.c
     src/http_parser.c
     src/http_request.c
     src/path_utils.c
     src/string_utils.c
)

set( SOXY_HDR src/debug.h
     src/file_utils.h
     src/http_client.h
     src/http_parser.h
     src/http_request.h
     src/logger.h
     src/path_utils.h
     src/soxy_constantsh
     src/soxy.h
     src/string_utils.h
)

set( SOXY_BIN src/soxy.c )

add_executable(soxy ${SOXY_BIN})
add_dependencies(soxy LibUV)

项目结构:/soxy /soxy/CMakeLists.txt /soxy/build/

当我生成makefile并运行make,lib和include时,外部项目最终都在/soxy中,我希望它们显示在/soxy/build/中,而当它构建我的项目时,它不会设置包含权限,以使 #include< uv.h> 不起作用,并且是未解决的头文件包含.

When I build the makefile and run make, the lib and include for the external project end up in /soxy, I'd like them to show up in /soxy/build/ and when it builds my project it doesn't setup the include right such that #include <uv.h> doesn't work and is an unresolved header file inclusion.

推荐答案

嗯,您发现了几处错误.

Hmmm, you are getting several things wrong.

  • 您通常不需要定义头文件列表.
  • 您没有告诉任何地方编译 SOXY_SRC 变量中包含的文件.例如,如果您执行 add_executable(soxy $ {SOXY_BIN} $ {SOXY_SRC}),则可以编译它们.
  • add_dependencies 之后,您还需要 target_link_libraries .
  • 您应将目标定为源外"版本.也就是说,您不应有 $ {CMAKE_CURRENT_SOURCE_DIR}/build
  • 之类的东西
  • You usually don't need to define a list of header files.
  • You are not telling anywhere to compile the files contained in the SOXY_SRC variable. You would compile them for example if you did add_executable(soxy ${SOXY_BIN} ${SOXY_SRC}).
  • After add_dependencies you'll also need target_link_libraries.
  • You should target "out of source" build. That is, you shouldn't have stuff like ${CMAKE_CURRENT_SOURCE_DIR}/build

因此,请尝试以下操作(我不知道 ExternalProject_Add 命令,因此我只能猜测行为).像这样修改两行 ExternalProject_Add :

So, try the following (I don't know the ExternalProject_Add command so I can only guess the behaviour). Modify two lines of ExternalProject_Add like this:

BUILD_IN_SOURCE 0
PREFIX ${CMAKE_BINARY_DIR}/LibUV

并添加:

include_directories(${CMAKE_BINARY_DIR}/LibUV)

您可能还需要

link_directories(${CMAKE_BINARY_DIR}/LibUV)

理想情况下,LibUV的include目录不同于库目录,请查看您的有效地执行ExternalProject_Add .

Ideally the include directory of LibUV is different from the library directory, take a look to what your ExternalProject_Add effictively does.

如前所述,您可能需要 target_link_libraries(soxy uv)或类似的名称,具体取决于生成的库名

As said before, you'll probably need a target_link_libraries(soxy uv) or something similar, depending on the generated library name

这篇关于CMake-更改lib和include目录用于外部项目和正确链接的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆