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/

相關文章
相關標籤/搜索