CMake---基础练习2

# t2/ CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

#1
#PROJECT (HELLO)
#SET(SRC_LIST main.c)
#MESSAGE(STATUS "This is BINARY dir" ${HELLO_BINARY_DIR})
#MESSAGE(STATUS "This is SOURCE dir" ${HELLO_SOURCE_DIR})
#ADD_EXECUTABLE(hello ${SRC_LIST})

#2
#PROJECT(HELLO)
#ADD_EXECUTABLE(hello main.c)

PROJECT(HELLO)
ADD_SUBDIRECTORY(src bin)
//main.c

#include <stdio.h>
int main()
{
    printf("Hello World from t1 Main!
 ");
    
    return 0;
}
#  t2/src/CMakeLists.txt

ADD_EXECUTABLE(hello main.c)
u@u160406:~/learn_Cmake/cmake/t2$ ls
build  CMakeLists.txt  src
u@u160406:~/learn_Cmake/cmake/t2$ tree
.
├── build
├── CMakeLists.txt
└── src
    ├── CMakeLists.txt
    └── main.c

2 directories, 3 files
u@u160406:~/learn_Cmake/cmake/t2$ cd build
u@u160406:~/learn_Cmake/cmake/t2/build$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- 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
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/u/learn_Cmake/cmake/t2/build
u@u160406:~/learn_Cmake/cmake/t2/build$ make
Scanning dependencies of target hello
[ 50%] Building C object bin/CMakeFiles/hello.dir/main.c.o
[100%] Linking C executable hello
[100%] Built target hello
u@u160406:~/learn_Cmake/cmake/t2/build$ cd bin
u@u160406:~/learn_Cmake/cmake/t2/build/bin$ ./hello
Hello World from t1 Main!
 u@u160406:~/learn_Cmake/cmake/t2/build/bin$ 

原文地址:https://www.cnblogs.com/carle-09/p/11685332.html