[CMake] using source files in build directory

Johannes Zarl-Zierl johannes.zarl-zierl at jku.at
Fri Oct 23 05:07:05 EDT 2015


Hi,

For header files, that's quite a normal thing to do - you just need to add the 
CMAKE_CURRENT_BINARY_DIR to your include directories.

For .cpp files, you can write your target so that it is referencing the source 
file in the binary directory. I.e.:

configure_file(
  ${CMAKE_CURRENT_SOURCE_DIRECTORY}/file1.cpp.in
  ${CMAKE_CURRENT_BINARY_DIRECTORY}/file1.cpp COPYONLY)
add_executable( target1 ${CMAKE_CURRENT_BINARY_DIRECTORY}/file1.cpp)

Is that sufficient for your needs?

If you rather need a real wildcard way to override arbitrary source files, 
then you're probably out of luck. You could still write a wrapper around 
add_executable and add_library that inspects source directory and binary 
directory, but that might not be worth the effort...

  Johannes

On Thursday 22 October 2015 15:13:08 Srinath Vadlamani wrote:
> Hello All,
>   I am aware of out-of-source builds.  I want to add modified source files
> to the out of source build directory and have CMake use these modified
> files.
> 
> Below is my example:
> $pwd
> <source_dir>
> $ls
> CmakeLists.txt CMake README src tests doc ...
> $mkdir build1
> $cd build1
> $cp ../src/file1.cpp .
> $ <edit file1.ccp to make it so much better>
> $ccmake <list of options> <source_dir>
> $make -j N
> $ <run executable and learn it does what I want but slowly>
> 
> Now I want to make another build directory that houses a few other modified
> files:
> 
> $cd <source_dir>
> $mkdir build2
> $cd build2
> $cp ../src/file1.cpp . && cp ../src/file2.cpp .
> $<edit file1 and file2 to make them awesome and hopefully code runs fast>
> $ccmake <list of options> <soruce_dir>
> $make -j N
> $<run executable and learn that build1 modification was better for file1
> but file2 mods helped>
> 
> 
> ..... and so on.  The point is that the build directory allowing for
> modified source files helps preserve executables and the dependent source
> files for code management.  Some may now chime in with opinions on version
> control usage, but I'm trying to understand if it is possible to do what I
> described and is possible with Autotools.
> 
> Thank you for your time,
> <>Srinath
> 
> =================================
> Srinath Vadlamani
> =================================
> 
> On Thu, Oct 22, 2015 at 3:02 PM, Gonzalo <ggarra13 at gmail.com> wrote:
> > El 21/10/15 a las 17:45, Srinath Vadlamani escribió:
> > 
> > Hello All,
> > 
> >   Is it possible to have CMake use source files in some particular order
> > 
> > that are placed in the out-of-source build directory?  This is allows for
> > the convenience of having multiple builds in different build directories
> > due to a few file changes while preserving the files themselves?
> > 
> > You are aware that you can do out of source builds, right?
> > 
> > $ mkdir build-linux64
> > $ cd build-linux64
> > $ cmake .. # plus any options you want
> > $ make
> > 
> > --
> > Gonzalo Garramuñoggarra13 at gmail.com



More information about the CMake mailing list