[CMake] Compiling object files.

Johannes Zarl johannes.zarl at jku.at
Wed Apr 3 11:44:59 EDT 2013


Hi Daniel,

On Wednesday, 3. April 2013, 16:06:36, Daniel Carrera wrote:
> Summary: How do I use CMake to compile object files? Or is this the
> wrong question to ask? (i.e. *should* I be compiling object files?)

Short answer: normally you don't and you shouldn't add statements to compile 
object files.

CMake is a build system generator, not a build-system in itself. I.e. you only 
tell cmake "I want to build library A from source files a.cpp and b.cpp, and I 
want to link my application C , which is created from c.cpp, to the library 
A". CMake then generates the build system (e.g. Makefiles or a Visual Studio 
solution) for you.

> Details:
> ---------
> I am starting to learn CMake, so I am not even sure I am formulating
> my questions the right way... I have a large Makefile that I want to
> try to convert to CMake. I did not write the Makefile and I know
> little about build systems. But I am interested in CMake and I want to
> experiment anyway. 

If you have problems with the terminology and concepts behind cmake, I would 
recommend browsing through the wiki ( 
http://www.cmake.org/Wiki/CMake#Basic_Introductions ). While it can be 
overwhelming at first, there is a wealth of information to be found there.

> My first question is how to convert something like
> this:
> 
> cparam.o:  cparam.f90 cparam.local cparam.inc cparam_pencils.inc
>         if [ "" ]; then \
>             rm -f cparam.inc; \
>             rm -f cparam_pencils.inc; \
>             ln -s cparam.inc cparam.inc; \
>             ln -s cparam_pencils.inc cparam_pencils.inc; \
>         fi
>         $(FC) $(FFLAGS) $(FFLAGS_GENERAL) $(F90FLAGS) -o cparam.o -c
> cparam.f90
> 
> 
> I cannot imagine what the if statement could possibly accomplish, but
> I trust that CMake would make the whole thing unnecessary. 

The if statement is effectively just a block-comment. You can just ignore it 
and its contents.

> So I figure
> ("hope") that all I have to do is tell CMake to make the object file
> cparam.o. The problem is that I cannot find anything in the
> instructions for making object files. All I see is commands like:
> 
> 
> add_executable(hello  hello.f90 world.f90)
> 
> add_library(particles backend.f90 vector.f90 const.f90)
> 
> 
> There is no "add_object" function or anything like it... Maybe I am
> going about it the wrong way. Maybe I should not be thinking in terms
> of making object files at all. Perhaps I should be planning to do
> something like:
> 
> add_executable(myprogram ${HUGE_LIST_OF_200_FILES})

That's the right way to go. Although if you have over 200 input files, you 
might want to group them into libraries if that's appropriate.

> My worry with this approach is that I see no room to convert the
> Makefile incrementally. I was thinking of compiling just one object
> file, check that that works and has the right symbols, and then move
> to the next object file. Whereas this other option looks like an "all
> or nothing" proposition.

Converting incrementally can't be done IMO (at least not without significant 
overhead). Since you would probably use the Makefile generator of cmake, you 
would probably just end up with cmake overwriting your makefiles or similar 
problems.

I hope that cleared up some of the confusion. Please do ask again if there are 
problems...

  Johannes


More information about the CMake mailing list