Phil CK

CMake - Scratch Solution

Last updated on

I like unit testing facilities just as a place to develop code, but other things can be problematic, like writting UI test code, I tend to call this stuff scratch code, its useful for a limited period but after that … meh …

In my main hobby solution, I have a project that gets generated as just a main file so I can write some scratch code.

make_directory(scratch_code/cpp)
set(SCRATCH_CPP_DEPS libs to link to scratch project)

file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/scratch_code/cpp/main.cpp "/* SCRATCH: C++11\n * WARNING: autogenerated by Cmake, your code may vanish! \n * LINKED WITH: ${SCRATCH_CPP_DEPS} \n */\n\nint main(int argc, const char **argv) {\n        return 0;\n};\n")
add_executable(scratch_cpp ${CMAKE_CURRENT_SOURCE_DIR}/scratch_code/cpp/main.cpp)
target_link_libraries(scratch_cpp LINK_PUBLIC ${SCRATCH_CPP_DEPS})
set_target_properties(scratch_cpp PROPERTIES FOLDER extras)
set_property(TARGET scratch_cpp PROPERTY CMAKE_CXX_STANDARD 11)

This gives me a temp project to write some code in and ignore (just remember to add it to your .gitignore or what ever source control you use.)