This is the fifth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

After working with tensorflow and pytorch, I wanted to implement a simple c++ based deep learning framework to help me understand these algorithms.

Create the project and set up the executable

The most basic project is to build source code files into executable files. For a simple project, create a file named cMakelists.txt and add the following three lines to the file. This will be the starting point for learning CMake.

Cmake_minimum_required (VERSION 3.10)
#Set the project name
project(Tutorial)

#Add executable files
add_executable(Tutorial main.cppc)
Copy the code

Set the project name

Note that this example uses lowercase commands in the cMakelists.txt file. CMake supports uppercase, lowercase, and mixed-case commands. The source code for main. CPP is provided in the Demo directory or can be placed in the Build directory.

The first thing to do is to create a project and then execute an executable file for the project. While this can be done entirely in source code, using cMakelists.txt provides more flexibility in setting the project name and version number as the project is built. In the cMakelists.txt file, use the project() command to set the project name and version number.

project(Tutorial)
Copy the code

Then, you configure a header file that passes the version number to the source code.

Configure the version number of the project

Since the configured files will be written to the binary tree, you need to add this directory to the list of paths to search for containing files. Add the following lines at the end of the cMakelists.txt file. Create the tutorialconfig.h.id file in the project directory, and enter the following in the file

// Configure options and set Tutorial major and minor version numbers
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
Copy the code

When CMake configes this header, the @tutorial_version_major @ and @tutorial_version_minor @ values are replaced. You can create and set some variables in the cMakelists. TXT file, that is, cMakelists. TXT will replace variables wrapped in the @ symbol in the call to tutorialconfig.h.id

set(Tutorial_VERSION_MAJOR 1)
set(Tutorial_VERSION_MINOR 0)
Copy the code

Next, modify the main.cpp file to include tutorialconfig. h in the header. The automatically created tutorialconfig. h will also appear in the build directory.

#include <iostream>
#include "TutorialConfig.h"
Copy the code

This allows main.cpp to print out the name and version number of the executable file, as shown below

cout <<  Tutorial_VERSION_MAJOR << endl;
Copy the code
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)

project(Tutorial)

set(Tutorial_VERSION_MAJOR 1)
set(Tutorial_VERSION_MINOR 0)

configure_file(TutorialConfig.h.in TutorialConfig.h)

add_executable(Tutorial main.cpp)

target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
Copy the code
main.cpp
#include <iostream>
#include "TutorialConfig.h"
using namespace std;

int main(int argc, char const *argv[])
{
    // cout << "hello world" << endl;
    cout <<  Tutorial_VERSION_MAJOR << endl;
    // cout << Tutorial_VERSION_MINOR << endl;
    return 0;
}
Copy the code

Specifies the c++ standard library

# execute the C++ standard libraryset(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
Copy the code

Now let’s replace atof with STD ::stod in main. CPP to add some C++11 features to our project. Also remove #include

.

#include <iostream>
#include "TutorialConfig.h"
using namespace std;

int main(int argc, char const *argv[])
{
    // cout << "hello world" << endl;
    cout <<  std::stod(argv[1]) * 10<< endl;
    // cout << Tutorial_VERSION_MINOR << endl;
    return 0;
}
Copy the code

We will need to explicitly state in the CMake code which version of the C++ standard library to compile. The easiest way is to use the CMAKE_CXX_STANDARD variable to set up the C++ standard library. Here we set the CMAKE_CXX_STANDARD variable in the cmakelists. TXT file to 11, and when CMAKE_CXX_STANDARD_REQUIRED is set to True, The CMAKE_CXX_STANDARD specified version is referenced to the CPP file for add_executable.

Run CMake

Run the cmake executable and then build the project with the build tool of your choice. Next go to the build directory build and run CMake to configure the project and generate a local build system.

cmake ..
Copy the code

Use cmake — build. to call the build system to compile and link the project.

cmake --build .
Copy the code

If we can run the application in build by executing./Tutorial, we can see the effect.

Add your own implementation lib to your project

What you need to do now is to add a library to your project that provides a square root method for calculating numbers. To explain that this library is not a standard square root function provided by the compiler, but a library implemented by yourself, then see how to add it to your project during compilation.

We then create a subdirectory of MathFunctions to put our library files in. This directory already contains a header file, mathfunctions.h, and a source file, main.cpp. In the source file, define a function named add that provides functionality similar to that of the compiler’s add function.

Create a mathfunctions.h file in the MathFunctions directory, which is kind of like the interface, the CPP header file

MathFunctions/MathFunctions.h
double add(double x, double y);
Copy the code

In the MathFunctions directory, create a main.cpp file that will implement the add method.

MathFunctions/main.cpp
#include "MathFunctions.h"

double add(double x, double y){
    return x + y;
}
Copy the code

Then in the MathFunctions directory, create a cMakelists.txt file as follows

MathFunctions/CMakeLists.txt
add_library(MathFunctions main.cpp)
Copy the code

It is understandable to add a package to your project. MathFunctions is the name of the package, and main.cpp can package the entry file for export.

main.cpp
#include <iostream>
#include "TutorialConfig.h"
#include "MathFunctions.h"

using namespace std;

int main(int argc, char const *argv[])
{
    // cout << "hello world" << endl;
    double inputValue = std::stod(argv[1]);
    // cout << std::stod(argv[1])<< endl;
    const double outputValue = add(inputValue,2.0);
    
    cout <<  outputValue << endl;
    // cout << Tutorial_VERSION_MINOR << endl;
    return 0;
}

Copy the code

Add an add_subdirectory() to the cMakelists.txt file to call cMakelists.txt in the MathFunctions directory to build the library. Also add to the executable main.cpp and add MathFunctions to the include directory so that you can find the main.cpp project main file and import it into the mathfunctions.h header file.

CMakeLists.txt
Cmake_minimum_required (VERSION 3.10) project(Tutorial) # specify the C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) set(Tutorial_VERSION_MAJOR 1) set(Tutorial_VERSION_MINOR 0) set(Tutorial_NAME_VERSION 1) configure_file(TutorialConfig.h.in TutorialConfig.h) add_subdirectory(MathFunctions) add_executable(Tutorial main.cpp) target_link_libraries(Tutorial PUBLIC MathFunctions) target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/MathFunctions" )Copy the code
add_subdirectory(MathFunctions)
Copy the code
  • target_link_libraries
  • target_include_directories
target_link_libraries(Tutorial PUBLIC MathFunctions)
target_include_directories(Tutorial PUBLIC
                          "${PROJECT_BINARY_DIR}"
                          "${PROJECT_SOURCE_DIR}/MathFunctions"
                          )
Copy the code