主要是在使用 SystemC 的时候遇到的,一般 C++ 的文件组织形式都是 h
文件放在 include/
下,但是在使用 SystemC 时,每个 .h
和 .cpp
都是成套出现的,这里就想要按照每个 module 一起的组织的方式。
即想组织成如下结构
1 2 3 4 5 6 7 8 9 10 11
| . ├── main.cpp ├── CMakeLists.txt ├── Module1 │ ├── module1.h │ ├── module1.cpp │ └── CMakeLists.txt └── Module2 ├── module2.h ├── module2.cpp └── CMakeLists.txt
|
下面开始吧
文件组织
1 2 3 4 5 6 7 8 9 10 11
| . ├── main.cpp ├── CMakeLists.txt ├── hello │ ├── CMakeLists.txt │ ├── hello.cpp │ └── hello.h └── world ├── CMakeLists.txt ├── world.cpp └── world.h
|
顶层目录
1 2 3 4 5 6 7 8 9 10
| #include "hello.h" #include "world.h" #include <stdio.h>
int main() { hello(); world(); return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
cmake_minimum_required(VERSION 2.8) project(helloworld)
aux_source_directory(. DIRSRCS)
include_directories( ./ ./hello ./world )
add_subdirectory(hello) add_subdirectory(world)
add_executable(helloworld ${DIRSRCS}) target_link_libraries(helloworld hello world)
|
hello/
1 2 3 4 5 6 7 8
| #ifndef HELLO_H #define HELLO_H
#include <stdio.h> void hello();
#endif
|
1 2 3
| #include "hello.h" void hello() { printf("hello\n"); }
|
1 2 3 4
|
aux_source_directory(. DIR_HELLO_SRCS) add_library(hello ${DIR_HELLO_SRCS})
|
world/
1 2 3 4 5 6 7 8
| #ifndef WORLD_H #define WORLD_H
#include <stdio.h> void world();
#endif
|
1 2 3
| #include "world.h" void world() { printf("world\n"); }
|
1 2 3 4
|
aux_source_directory(. DIR_WORLD_SRCS) add_library(world ${DIR_WORLD_SRCS})
|
原理
其实主要看其中的三个 CMakeLists.txt
的文件就可以了
在子模块中的 CMake
就是把当前目录下所有文件写入对应名字的library
中,如hello
、world
然后顶层目录文件需要做三件事
include_directories
:将 #include
的目录放进来:
add_subdirectory
:将刚才写好的library
放进来(找到对应目录下的 sub-library)
target_link_libraries
:将刚才的library
和主文件链接上
即可