Cantaff0rd
Cantaff0rd

Reputation: 723

Running terminal command from cmake script

I'm trying to do this in Cmake:

In the if which checks for the mentioned condition I tried running 'sudo make install' so I do not have to manually do that after running 'cmake' command. However if I add that 'execute_process' there I get the error: 'make: *** No rule to make target 'install'. '. Please tell me what am I doing wrong and how can I fix this so the command 'make install' also gets ran when I generate the makefiles with 'cmake' command. Thank you. Here is the code:

cmake_minimum_required(VERSION 3.0)
project (TestLibrary)

include_directories(include)
set(SOURCES src/Student.cpp)
add_library(testStudent SHARED ${SOURCES})

if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Generated_Library/libtestStudent.so)
 install(TARGETS testStudent DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/Generated_Library)
 execute_process(COMMAND sudo make install
                 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/build)
endif()

Upvotes: 1

Views: 952

Answers (1)

Samuel Peter
Samuel Peter

Reputation: 4166

execute_process runs its command at configuration time, before CMake generates the build system. When the command runs, there is no Makefile, hence the error you're seeing.

CMake isn't designed to be used as a unique command that does all of the build, but only to generate a build system (Makefile or otherwise) as the first step in a build process. What you can do to get a unique build command is to make a build.sh script which runs CMake with appropriate arguments, then make and optionally make install.

Upvotes: 1

Related Questions