Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create include file using CMake

Tags:

c++

cmake

I have a folder containing hpp and cpp files. I need to create a header file that includes all hpp files in the said folder:

#pragma once
#include "test_1.hpp"
#include "test_2.hpp"
#include "test_3.hpp"

can CMake do that?

Note:I am not going to put 'my' research work on your shoulder. I just need to know if such thing is possible, and possibly a link to where I can read from. My initial googling didn't show anything useful. thank you

like image 886
rahman Avatar asked Oct 20 '25 17:10

rahman


1 Answers

You can use configure_file to fill a variable inside a template file. Create a template file containing the outline of the desired file and a placeholder for your include statements, e.g.:

list.hpp.in

#pragma once
@include_statements@

Then, in your CMakeLists.txt, you can fill the placeholder @include_statements@ with a list of files wrapped with #include statements.

project(test)
cmake_minimum_required(VERSION 2.8)

# Get list of some *.hpp files in folder include 
file(GLOB include_files include/*.hpp)

# Convert the list of files into #includes
foreach(include_file ${include_files})
  set(include_statements "${include_statements}#include \"${include_file}\"\n")
endforeach()

# Fill the template    
configure_file(list.hpp.in list.hpp)
like image 70
Andrzej Pronobis Avatar answered Oct 23 '25 07:10

Andrzej Pronobis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!