• cmake 及make 实践记录


    DEBIAN操作系统

    预备操作:

    安装 gcc g++ make cmake 

    开启Terminal 

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

    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.
    

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

    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

    最后结果:

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

      内容解释参考

    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/

  • 相关阅读:
    hdu 6435 Problem J. CSGO 最长曼哈顿距离+二进制枚举
    UVA11419 SAM I AM 二分图最小点覆盖
    数论入门基础整理
    [算法导论]#1 摊还分析
    [转载]markown语法
    [AC自动机]玄武密码
    [学习笔记]AC自动机
    分火腿
    [学习笔记]二分与分治
    [学习笔记]二项式反演
  • 原文地址:https://www.cnblogs.com/itdef/p/5767940.html
Copyright © 2020-2023  润新知