所以我决定尝试学习和使用CMake,这样我就可以为Windows,Linux和MacOS制作一个可移植的API。
我有我想要的两部分目录树:
/
PsychoEngine/
includes/
Engine/
PsychoEngine.hpp
src/
PsychoEngine/
Engine/
PsychoEngine.cpp
dir_engine.cmake
dir_pe.cmake
dir_src.cmake
CMakeLists.txt
Temp_PE_Game/
includes/
src/
Main/
TempPEGameMain.cpp
dir_main.cmake
dir_src.cmake
CMakeLists.txt
CMakeLists.txt
所以Temp_PE_Game被设置为可执行文件,以便我可以测试我在PsychoEngine中编写的所有内容。 AFAIK,用于构建和运行Temp的CMake脚本是正确的,它运行正常。
然而,PsychoEngine,是我在努力的地方。目前我的“root”CMakeLists脚本是:
cmake_minimum_required(VERSION 3.0)
project(PsychoEngine)
message("Building PsychoEngine Library.")
set(PROJECT_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(PROJECT_SRC_FILES ${PROJECT_SRC_DIR})
include_directories(${PROJECT_SRC_DIR}/includes
${CMAKE_BINARY_DIR}/exports/)
include(${PROJECT_SRC_DIR}/src/dir_src.cmake)
option(BUILD_SHARED_LIBS "Build Shared Libraries" ON)
set(LIB_TYPE STATIC)
if(BUILD_SHARED_LIBS)
set(LIB_TYPE SHARED)
endif(BUILD_SHARED_LIBS)
include(GenerateExportHeader)
add_library(PsychoEngine ${LIB_TYPE} ${PROJECT_SRC_FILES})
GENERATE_EXPORT_HEADER(PsychoEngine
BASE_NAME PsychoEngine
EXPORT_MACRO_NAME PSYCHOENGINE_EXPORT
EXPORT_FILE_NAME ${CMAKE_BINARY_DIR}/exports/PsychoEngineExports.hpp
STATIC_DEFINE PsychoEngine_BUILT_AS_STATIC)
这似乎也有效。但是,当我尝试在我的PsychoEngine.hpp中导入PsychoEngineExports.hpp时:
#include "PsychoEngineExports.hpp" /* PEExports.hpp */
#include <iostream>
namespace pe { namespace engine {
class PSYCHOENGINE_EXPORT PsychoEngine {
public:
PsychoEngine();
int start();
};
}}
Visual Studio的intellisense开始抱怨它无法找到PEExports.hpp,并且类定义间歇性地以红色显示。
我想,哦,可能只是因为PEExports.hpp还没有生成,所以我在我的构建目录中重新运行了cmake,它仍然无效。
这是我的Temp_PE_Game CMakeLists.txt:
message("Building PE Temp Game.")
set(PROJECT_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(PROJECT_SRC_FILES ${PROJECT_SRC_DIR})
# Include PsychoEngine's headers
include_directories(${CMAKE_SOURCE_DIR}/PsychoEngine/includes)
include_directories(${PROJECT_SRC_DIR}/includes)
message("PE Game Binary Dir: " ${CMAKE_BINARY_DIR})
message("PE Game Executable DIR: " ${EXECUTABLE_OUTPUT_PATH})
include(${PROJECT_SRC_DIR}/src/dir_src.cmake)
add_executable(Temp_PE_Game ${PROJECT_SRC_FILES})
有什么想法吗?
干杯!