2013-02-23 32 views
-1

我有一个问题是这个正确的应用程序结构?我从C++和CMake开始,我想以正确的方式开始。它是正确的C++应用程序结构?

Root 
|--build 
|--src 
| |-Application 
| | |-------App.cpp 
| | |-------App.h 
| | |-------CMakeLists.txt 
| |-Logger 
| | |--Some files 
| | |--CMakeLists 
| |-CMakeLists.txt 
| |-main.cpp 
|--CMakeLists.txt 

根\的CMakeLists.txt

cmake_minimum_required (VERSION 2.6 FATAL_ERROR) 
# set (CMAKE_VERBOSE_MAKEFILE ON) 

set (APP_NAME "sdl-example") 

project (${APP_NAME}) 

# /////////////////////// 
# /// Check build dir /// 
# /////////////////////// 

set (Build_Dir_OK "TRUE") 
string (REGEX MATCH "^${CMAKE_SOURCE_DIR}" In_Sub_Dir ${CMAKE_BINARY_DIR}) 
if (In_Sub_Dir) 
    string (REGEX MATCH "^${CMAKE_SOURCE_DIR}/build" In_Build_Dir ${CMAKE_BINARY_DIR}) 
    if (NOT In_Build_Dir) 
     set (Build_Dir_OK "FALSE") 
    endif() 
endif() 

if (NOT Build_Dir_OK) 
    message (FATAL_ERROR "You must run cmake from a directory that is not in your source tree, or that is in a special subdirectory of the tree whose name begins with ‘build’.") 
endif() 


# /////////////////////////// 
# /// Initializing OpenGL /// 
# /////////////////////////// 

find_package(OpenGL) 
if (NOT OPENGL_FOUND) 
    message (FATAL_ERROR "OpenGL not found") 
else (NOT OPENGL_FOUND) 
    message ("OpenGL correctly found") 
endif (NOT OPENGL_FOUND) 

include_directories(
    ${OPENGL_INCLUDE_DIRS} 
    ${INCLUDE_DIRECTORIES} 
) 

# //////////////////////// 
# /// Initializing SDL /// 
# //////////////////////// 

find_package(SDL) 
if (NOT SDL_FOUND) 
    message (FATAL_ERROR "SDL not found!") 
else (NOT SDL_FOUND) 
    message ("SDL correctly found") 
endif (NOT SDL_FOUND) 

include_directories(
    ${SDL_INCLUDE_DIR} 
    ${INCLUDE_DIRECTORIES} 
) 

set (LIBRARIES ${OPENGL_LIBRARIES} ${SDL_LIBRARY}) 

add_subdirectory(src) 

根的\ src \的CMakeLists.txt

if (APP_NAME STREQUAL "") 
    message (FATAL_ERROR "You must set the COMPONENT_NAME variable!") 
endif() 

message ("Preparing application: " ${APP_NAME}) 

file (GLOB SRCS ./*.c ./*.cpp) 
file (GLOB HDRS ./*.h ./*.hpp) 

include_directories (${CMAKE_CURRENT_SOURCE_DIR}) 

# ///////////////////////// 
# /// Adding executable /// 
# ///////////////////////// 

add_executable (
    ${APP_NAME} 
    ${SRCS} 
    ${HDRS} 
) 

set (COMPONENT_NAME "application") 
set (COMPONENTS ${COMPONENTS} ${COMPONENT_NAME}) 
add_subdirectory("Application") 

target_link_libraries(
    ${APP_NAME} 
    ${LIBRARIES} 
    ${COMPONENTS} 
    ${TARGET_LINK_LIBRARIES} 
) 

根的\ src \应用

if (COMPONENT_NAME STREQUAL "") 
    message (FATAL_ERROR "You must set the COMPONENT_NAME variable!") 
endif() 

message ("Preparing component:" ${COMPONENT_NAME}) 

file (GLOB SRCS ./*.c ./*.cpp) 
file (GLOB HDRS ./*.h ./*.hpp) 

add_library(
    ${COMPONENT_NAME} 
    ${SRCS} 
    ${HDRS} 
) 

target_link_libraries (
    ${COMPONENT_NAME} 
    ${LIBRARIES} 
    ${SDL_LIBRARY} 
) 
+3

正确的应用程序结构确实做*什么*? – 2013-02-23 18:20:06

+0

好的,对不起,定义不好。我认为易于维护(添加新组件,库等)。 – user1607808 2013-02-23 18:25:11

回答

2

没有“正确'的方式来做到这一点。唯一的规则就是尽一切可能让你最容易维护。您的目录结构看起来是从您提供的少量信息中找到的。

如果您发现您正在使用的目录结构导致您感到痛苦或难以保持组织性,您可以随时对文件进行洗牌。

+0

谢谢你,求助。 – user1607808 2013-02-23 19:36:55