[CMake] Help debug crazy Windows MSVC issue

Paul Smith paul at mad-scientist.net
Sun Feb 16 21:35:19 EST 2014


I'm using CMake 2.8.12.1 on Linux, MacOS, and Windows.  On Windows I'm
using MSVC 2010 and the Visual Studio generator, but I have only remote
access so I'm invoking my builds using devenv (not running Visual
Studio).

I'm deeply involved in some messy hackery related to building the same
code as a static library and as a DLL, and ensuring that the export and
import decorations are correct etc.  This is further complicated by the
fact that some of the source files are themselves generated.

This is all working great on Linux (Makefile generator) and MacOS (Xcode
and Makefile generators).  But it's completely unreliable on Windows
(Visual Studio generator).

What happens on Windows is that the generator runs TWICE, not once, and
in preparation for running the second time it deletes all the output
from the first run, and so if some other part of the build is trying to
compile and access some of the generated code at that time, the build
fails.  If it happens that no other part of the build needs to access
the generated content while the second run is generating it, then the
build succeeds.

I need some help or pointers or tips in how to figure out the problem;
I'm really not a Windows person.

What I've got is something like this:

  set(GENERATED ..list of generated .cpp and .h files..)

  set_source_files_properties(${GENERATED} PROPERTIES GENERATED TRUE)

  add_custom_command(OUTPUT ${GENERATED}
      COMMAND ...
      DEPENDS ...
      COMMENT "Generating output")

  add_library(Generated OBJECT ${GENERATED})

  add_library(BaseShared_OBJECTS OBJECT file1.cpp file2.cpp ...)
  add_library(BaseShared SHARED dummy.cpp $<TARGET_OBJECTS:Generated> $<TARGET_OBJECTS:BaseShared_OBJECTS)
  add_dependencies(BaseShared BaseShared_OBJECTS)

  add_library(BaseStatic_OBJECTS OBJECT file1.cpp file2.cpp ...)
  add_library(BaseStatic STATIC dummy.cpp $<TARGET_OBJECTS:Generated> $<TARGET_OBJECTS:BaseShared_OBJECTS)
  add_dependencies(BaseShared BaseShared_OBJECTS)

I originally just had the two libraries contain the ${GENERATED} source
directly, but had this duplicate build issue so I tried to create an
OBJECT library and have that library as the prerequisite.  However it
still seems to happen.

The reason I need to use the OBJECT libraries for BaseShared and
BaseStatic is that I need to link these into other shared libraries as
well.

Anyway, I can see in my build output the first "Generating output", and
the output is generated and all is compiling along nicely... then later
on I see ANOTHER "Generating output" and all heck breaks loose.


I just can't figure out what's going on and I don't know how to work
with/debug Visual Studio project files (if this was a makefile generator
I'd just read the makefile).

Some concrete questions (please remember I'm running devenv via a
terminal: I don't have ready access to Visual Studio):

(A) Can I force the build to run serially?  Or is there some way I can
more easily see what forces the second generator to run?

(B) Can I get the command lines for the compiler and linker, etc. to be
shown?  I tried setting CMAKE_VERBOSE_MAKEFILE but that didn't help.

(C) Any other ideas about what I can do to make this simpler and more
reliable?



More information about the CMake mailing list