1

I have three programs, for example: program_1.cpp, program_2.cpp and program_3.cpp.

As of now, I have separate makefiles to build these programs.

I would like to implement in this in the below manner.

cmake "program_1" # program_1.cpp has to compile cmake "program_2" # program_2.cpp has to compile cmake "program_3" # program_3.cpp has to compile 

Could you suggest me the ways to achieve this?


[UPDATE]:

I am using a shell script in order to compile the program. For example

bash build_script.sh build 

This would compile the file program_1.cpp and generates the executables.

In the same manner, I would like to implement the below approach.

bash build_script.sh build program_1 -- build program_1.cpp bash build_script.sh build program_2 -- build program_2.cpp bash build_script.sh build program_3 -- build program_3.cpp 
2
  • Just to be clear: do you want to have cmake conditionally only generate makefiles for one program, or do you want to be able to compile only one program at a time from the makefiles generated by cmake? Commented Jul 24, 2018 at 2:31
  • Both the approaches should be fine assuming that only one program runs at a time when I give cmake "program_1".... Commented Jul 24, 2018 at 5:43

2 Answers 2

3

When generating makefiles, cmake creates a make target for each add_executable() call, so it's only matter of using make target on the makefiles that are generated by cmake:

CMakeLists.txt:

... add_executable(program_1 prog1.cpp) add_executable(program_2 prog2.cpp) add_executable(program_3 prog3.cpp) 

And then when comes time to build:

cmake path/to/source/dir make program_1 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the steps. I got the clear picture how to select particular program while compiling. I will be updating the question. Please check.
2

Assuming you meant three different targets to compile, you can have a simple makefile:

.PHONY program_1: program_1: gcc -o program_1 program_1.cpp .PHONY program_2: program_2: gcc -o program_2 program_2.cpp .PHONY program_3: program_3: gcc -o program_3 program_3.cpp 

Then you can simply do:

make program_2 

To compile just program_2. This has nothing to do with cmake however.

1 Comment

Thanks for the answers. Will try the method and let you know.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.