Compile a single file in a CMake project?

I am developing a C ++ project, which will be enclosed in a larger one.

I saw that in a larger project (this is a Qt application and it is built from qmake) I can compile one file from the linux command line by simply entering the relative path to a specific file as an argument to do.

On the other hand, I use CMake for my own project. When I change the code for a compilation unit and I need to change its header file, I have to wait a long time to compile its dependencies, and then its own source file. But there are some situations in which I would prefer to check if the source code compiles in the * .cc file without errors.

Is there a way to create a Makefile from CMake, how does qmake do it? Switching to qmake is no longer an option.

+6
source share
3 answers

No, CMake does not offer built-in support for compiling individual files.

You should add a target for each object file, possibly using a function that repeats across all the catalog files.

+3
source

You do not need to add additional custom targets to your CMake scripts, since the Makefiles generated by CMake already contain .o targets for each .cc file. For instance. if you have a source file called mySourceFile.cc , there will be a Makefile in your build directory that defines the target <Some Path>/mySourceFile.cc.o . If you cd into your build directory, you can use grep or ack-grep to find the Makefile that defines this target, then cd into this Makefile and create it.

eg. suppose the ack-grep mySourceFile.cc.o prints something like:

 foo/bar/Makefile 119:x/y/z/mySourceFile.o: x/y/z/mySourceFile.cc.o 123:x/y/z/mySourceFile.cc.o: 124: # recipe for building target 

Then you can build mySourceFile.cc.o by doing:

 cd foo/bar && make x/y/z/mySourceFile.cc.o 
+7
source

Not out of the box. CMake does not expose these "internal" makefile rules in the main makefile.

You can only do this if you consider what file structure CMake uses internally. You can, for example, to compile single .obj files using Makefile from CMake

 make -f CMakeFiles/myProg.dir/build.make CMakeFiles/myProg.dir/main.cc.obj 

when you have something like

 cmake_minimum_required(VERSION 3.1) project(myProg CXX) file(WRITE "main.cc" "int main()\n{\nreturn 0;\n}") add_executable(myProg main.cc) 
+4
source

All Articles