Coder Social home page Coder Social logo

cmake2_envvars's Introduction

CMake Tutorial - Part 2. Environment Variables

CMake has build in defined environment variables that can be used for modifying the behavior of CMake.

Writing out the environment variable

Let us look at the `CMakeLists.txt' file that we created part 1.

cmake_minimum_required(VERSION 3.0.0)
project(hello)
add_executable(${PROJECT_NAME} hello.cpp)

With the line project(hello) we have set the environment variable PROJECT_NAME. We can see the effect by printing out the environment variable.

cmake_minimum_required(VERSION 3.0.0)

message("Project Name: ${PROJECT_NAME}")
project(hello)
message("Project Name: ${PROJECT_NAME}")

add_executable(${PROJECT_NAME} hello.cpp)

The output will be

Project Name:
Project Name: hello

Setting Environment Variables

You can set environment variables using the set command. Here we use set to set PROJECT_NAME.

cmake_minimum_required(VERSION 3.0.0)
set(PROJECT_NAME hello)
message("Project Name: ${PROJECT_NAME}")
add_executable(${PROJECT_NAME} hello.cpp)

Other Environment Variables

CMAKE_SOURCE_DIR

Let us consider the CMAKE_SOURCE_DIR environment variable. We can print out the directory where the CMakeLists.txt is located.

cmake_minimum_required(VERSION 3.0.0)
project(hello)
message(Source Dir: "${CMAKE_SOURCE_DIR}")
add_executable(${PROJECT_NAME} hello.cpp)

EXECUTABLE_OUTPUT_PATH

When we build the project, hello is created in the build directory. We can change where hello is created by changing the EXECUTABLE_OUTPUT_PATH.

CMAKE_BINARY_DIR is by default the top level of the build directory.

We want hello to be built in the bin directory inside the build directory.

cmake_minimum_required(VERSION 3.0.0)
project(hello)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable(${PROJECT_NAME} hello.cpp)

More environment variables are in the useful variables reference.

Other Environment Variables

We can obviously define our own environment variables. Of course, CMake will not do anything with them but they can be used as we would use variables for scripts.

For example, we can make HELLO_NAME as an environment variable and set that as the project name.

cmake_minimum_required(VERSION 3.0.0)
set(HELLO_NAME hello)
project(${HELLO_NAME})
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable(${PROJECT_NAME} hello.cpp)

Conclusion

In this tutorial we have described how to use environment variables to modify how CMake behaves.

cmake2_envvars's People

Contributors

mochan-b avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.