cmake 及make 实践记录

DEBIAN操做系统linux

预备操做:c++

安装 gcc g++ make cmake shell

开启Terminal ide

切换到超级用户 下载安装上述软件测试

A@debian:~$ su
Password: 

root@debian:/home/A# apt-get install gcc g++ make cmake
Reading package lists... Done
Building dependency tree       
Reading state information... Done
make is already the newest version.
gcc is already the newest version.
g++ is already the newest version.
cmake is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

  //==========================================================ui

makefile 内容以下操作系统

TARGET= main
CPP_FILES = $(shell ls *.cpp)
BASE = $(basename $(CPP_FILES))
OBJS = $(addsuffix .o, $(addprefix obj/,$(BASE)))
 
$(TARGET):$(OBJS)
	-rm -f $@
	g++ -o $(TARGET) $(OBJS)
 
obj/%.o:%.cpp
	@if test -d"obj";then\
		mkdir -p obj;\
	fi;\
	g++ -c -o $@ $<
 
clean:
	-rm -f $(TARGET)
	-rm -f obj/*.o

 同一目录下有三个文件 main.cpp Test1.cpp Test1.h Test2.cpp Test2.h.net

最后结果:orm

# make
rm -f main
g++ -o main obj/main.o obj/Test1.o obj/Test2.o

  内容解释参考blog

http://blog.csdn.net/wcl199274/article/details/39140459

因为网页排版 makefile内容请你们注意从新使用TAB排版 不然可能编译不过

//=============================================================

CMAKE的测试环境以下

一个main.cpp 内容随意 可编译便可

一个CMakeLists.txt

内容以下:

PROJECT(main)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS})

  编译结果以下

cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/A/Desktop/3

  

make
[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
Linking CXX executable main
[100%] Built target main

  //===================================================

更进一步的 根目录下 放入main.cpp CMakeLists.txt

在新建一个子目录src 子目录下放置Test1.cpp Test1.h CMakeLists.txt

根目录CMakeLists.txt内容以下:

PROJECT(main)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
ADD_SUBDIRECTORY( src )
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS}  )
TARGET_LINK_LIBRARIES( main Test )

  子目录CMakeLists.txt内容以下:

AUX_SOURCE_DIRECTORY(. DIR_TEST1_SRCS)
ADD_LIBRARY ( Test ${DIR_TEST1_SRCS})

  编译显示以下:

cmake .
-- The C compiler identification is GNU 4.9.2
-- The CXX compiler identification is GNU 4.9.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/A/Desktop/2

  

make
Scanning dependencies of target Test
[ 50%] Building CXX object src/CMakeFiles/Test.dir/Test1.cpp.o
Linking CXX static library libTest.a
[ 50%] Built target Test
Scanning dependencies of target main
[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
Linking CXX executable main
[100%] Built target main

  内容解释参考

http://www.ibm.com/developerworks/cn/linux/l-cn-cmake/

相关文章
相关标签/搜索