1

在我的项目中,我使用的是仅标头库 rapidjson v1.1.0。

└── my_project
    ├── CMakeLists.txt
    ├── src
    │
    ├── 3rdParty/tiny_dnn (header only)
    │   ├── CMakeLists.txt
    │   ├── src
    │   └── rapidjson_v0.2
    │   
    └── rapidjson_v1.1.0

现在的问题是tiny-dnn也包含了 rapidjson(但版本较旧),所以当我尝试在主 CMakeLists.txt 中包含 tiny_dnn 时,就像include_directories(${PROJECT_SOURCE_DIR}/3rdParty/tiny_dnn)tiny-dnn 搜索我的 rapidjson 或我的项目在 tiny 中搜索时出现的一些冲突-dnn 的 rapidjson。

my_project CMakeLists.txt

cmake_minimum_required(VERSION 3.9)
project(my_project)

# check the build type and set compiler and linker flags
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
IF(CMAKE_BUILD_TYPE MATCHES DEBUG)
    message("Debug build")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unknown-pragmas -g -O0 -std=c++17 -Ddeveloper_build")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -g -O0")

ELSEIF(CMAKE_BUILD_TYPE MATCHES RELEASE)
    message("Release build")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unknown-pragmas -O3 -std=c++17")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -O3")

ELSE()
    message(FATAL_ERROR "No build type specified")
ENDIF()

find_package(Boost COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS} ${PROJECT_SOURCE_DIR}/3rdParty/tiny-dnn)

file(GLOB_RECURSE SOURCE_FILES src/*.cpp src/*.c)

set(SOURCE_FILES
        ${SOURCE_FILES})

add_executable(my_project ${SOURCE_FILES})
target_link_libraries(my_project ${Boost_LIBRARIES} pthread)
set_target_properties(my_project PROPERTIES SUFFIX ${CMAKE_BUILD_TYPE})

我的项目.cpp

#include <tiny_dnn/tiny_dnn.h>
#include <rapidjson/rapidjson.h> // <- usr/local/include/rapidjson

int main(int argc, char **argv)
{
    rapidjson::Document d; // <- uses rapidjson (v0.2) of tiny_dnn/cereal/external/rapidjson but in my project i would use /usr/local/include/rapidjson (v1.0.1)

    return 0;
}
4

1 回答 1

0

一般来说 - 不要。强制一个客户使用更高/更好的。即使您绕过这些问题,您也可能会违反一个定义规则并在以后遇到段错误。

于 2020-09-22T12:13:17.947 回答