Skip to content

Commit

Permalink
Merge pull request #27 from chirsz-ever/improve-cmake-0629
Browse files Browse the repository at this point in the history
改进编译系统
  • Loading branch information
wysaid authored Jun 29, 2020
2 parents ed70112 + 6570862 commit 8624fe4
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 26 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*.VC.db
*.user
*.o
/build
.idea/
*-build-*

/build
/temp
51 changes: 50 additions & 1 deletion BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $ $env:PATH="C:\Dev-Cpp\MinGW64\bin;$env:PATH"
```
注意,CodeBloks 附带的 MinGW 只能在
[MSYS Makefiles 配置](<#### MSYS Makefiles 配置>)
[MSYS Makefiles 配置](<#-msys-makefiles-配置>)
下编译。在此建议您下载不附带 MinGW 的 CodeBlocks 并单独安装最新版 TDM-GCC64,
CodeBlocks 会自动识别已安装的 TDM-GCC。
Expand Down Expand Up @@ -150,3 +150,52 @@ $ cmake .. -G "Visual Studio 14 2015"
```sh
$ cmake .. -G "Visual Studio 14 2015 Win64"
```
## 编译临时测试文件
有时候为了测试新添加的功能,需要写一些测试用例,但编译安装修改后 EGE 再在项目外编译测试程序
比较麻烦,在项目里修改 CMake 配置并添加源文件和编译指令又会被 git 识别为未暂存的修改,会对 git
使用造成干扰。
项目 CMake 配置中已经写好了,开发者可以新建 `temp` 目录并添加源文件和 `CMakeLists.txt` 配置
文件,编译系统会自动配置,在编译 EGE 库后编译 `temp` 目录,而 temp 目录已被 `.gitignore`
排除在外,不会对 git 使用造成干扰。
例如,我们想要测试读取字符输入,于是新建 `temp` 目录,在 `temp` 目录下
新建 `CMakeLists.txt` 内容如下:
```cmake
add_executable(temp_test temp_test.cpp)
target_link_libraries(temp_test ${LIB_NAME})
```
新建 `temp/temp_test.cpp` 内容如下:
```cpp
#include <ege.h>
using namespace ege;
int main(int argc, char const *argv[])
{
initgraph(640, 480);
circle(120, 120, 100);
int i = 0;
while (is_run()) {
key_msg msg = getkey();
if (msg.msg == key_msg_char) {
xyprintf(0, i * 20, "%d", msg.key);
++i;
}
}
return 0;
}
```
执行前文所述编译步骤后在 `build/temp` 目录下就会生成可执行文件 `temp_test.exe`
25 changes: 9 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,18 @@ else ()
set(LIB_NAME "graphics${TARGET_BITS}")
endif ()

add_library(${LIB_NAME} STATIC)

add_executable(graphicstest)

add_subdirectory(src)

if (EXISTS temp)
add_subdirectory(temp)
endif ()

option(BUILD_DEMOS "Build demos" OFF)

if (BUILD_DEMOS)
file(
GLOB DEMO_SRCS
LIST_DIRECTORIES false
${PROJECT_SOURCE_DIR}/demo/*.cpp
)
foreach (DEMO_SRC ${DEMO_SRCS})
get_filename_component(DEMO_NAME ${DEMO_SRC} NAME_WE)
add_executable(${DEMO_NAME} ${DEMO_SRC})
target_include_directories(${DEMO_NAME} BEFORE
PRIVATE ${PROJECT_SOURCE_DIR}/src
)
target_link_libraries(${DEMO_NAME} ${LIB_NAME})
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_link_libraries(${DEMO_NAME} -mwindows)
endif ()
endforeach ()
add_subdirectory(demo)
endif ()
15 changes: 15 additions & 0 deletions demo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

file(
GLOB DEMO_SRCS
LIST_DIRECTORIES false
${PROJECT_SOURCE_DIR}/demo/*.cpp
)

foreach (DEMO_SRC ${DEMO_SRCS})
get_filename_component(DEMO_NAME ${DEMO_SRC} NAME_WE)
add_executable(${DEMO_NAME} ${DEMO_SRC})
target_link_libraries(${DEMO_NAME} ${LIB_NAME})
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_link_libraries(${DEMO_NAME} -mwindows)
endif ()
endforeach ()
22 changes: 14 additions & 8 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ set(EGE_CPP_SRC
math.cpp
)

add_library(${LIB_NAME} STATIC
${LPNG_SRC}
${EGE_CPP_SRC}
target_sources(${LIB_NAME}
PRIVATE ${LPNG_SRC}
PRIVATE ${EGE_CPP_SRC}
)

if (MSVC)
Expand All @@ -39,13 +39,19 @@ if (MSVC)
endif (MSVC)

# Just a hint
target_link_libraries(${LIB_NAME} gdiplus imm32 msimg32)
target_link_libraries(${LIB_NAME}
INTERFACE gdiplus
INTERFACE imm32
INTERFACE msimg32
)
target_include_directories(${LIB_NAME}
INTERFACE .
)

# compile test
add_executable(graphicstest graphicstest/maintest.cpp)

target_include_directories(graphicstest BEFORE
PRIVATE .)
target_sources(graphicstest
PRIVATE graphicstest/maintest.cpp
)

target_link_libraries(graphicstest ${LIB_NAME})
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
Expand Down

0 comments on commit 8624fe4

Please sign in to comment.