cmake compiles a simple c++ project of demo

  • 2020-07-21 09:25:36
  • OfStack

cmake compiles an c++ project to generate an executable file with a small example:

Organize the directory structure:

[

CMake Lists.txt addlib build main.cpp

]

There is no tree command on the computer, and I don't dare to take screenshots, so I will just take a look, where build is the path specified by cmake compilation, there is also a header file and source file under addlib, and there is also an ES16en.txt, you can see below:

[

CMake Lists.txt library.cpp library.h

]

First look at the code of each file:

addlib/library.h:


#ifndef CPPPROJECT_LIBRARY_H
#define CPPPROJECT_LIBRARY_H
 
int acc_add(int);
 
#endif

addlib/library.cpp:


#include "library.h"
#include <iostream>
 
void hello(){
  std::cout<<"hello world"<<std::endl;
}
 
 
int acc_add(int n){
  int sum=0;
  for(int i=0;i<n;i++){
    sum+=i;
  }
  return sum;
}

addlib/CMakeLists.txt:


cmake_minimum_required(VERSION 3.10)
project(accliblibrary)
 
set(CMAKE_CXX_STANDARD 11)
 
add_library(accliblibrary SHARED library.cpp library.h) #  V. to generate 1 Three library files. 

main.cpp:


#include <iostream>
#include <string>
#include "addlib/library.h"
using namespace std;
int main(){
 int n=10;
 int ans=acc_add(n);
 cout<<"1+....+"<<n<<"="<<ans<<endl;
  return 0;
}

CMakeLists.txt:


cmake_minimum_required(VERSION 3.10)
project(cppproject)
 
set(CMAKE_CXX_STANDARD 11)
 
add_subdirectory(acclib)
 
 
add_executable(cppproject main.cpp) # generate 1 Three executable files 
 
target_link_libraries(cppproject accliblibrary)

Here's how to compile the project to generate the executable:

[

cd build/

cmake ..

][

-- The C compiler identification is AppleClang 8.0.0.8000042
-- The CXX compiler identification is AppleClang 8.0.0.8000042
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/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: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/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: /Users/zhoumeixu/Documents/cmakedemo/build

]

make:

[

Scanning dependencies of target addliblibrary
[ 25%] Building CXX object addlib/CMakeFiles/addliblibrary.dir/library.cpp.o
[ 50%] Linking CXX shared library libaddliblibrary.dylib
[ 50%] Built target addliblibrary
Scanning dependencies of target cmakedemo
[ 75%] Building CXX object CMakeFiles/cmakedemo.dir/main.cpp.o
[100%] Linking CXX executable cmakedemo
[100%] Built target cmakedemo

]

./cmakedemo

[

1+....+10=45

]

This article on the following, the following site this site will continue to be introduced for you


Related articles: