cmake - extracting pdb files from object libraries

2019-06-15 21:10发布

问题:

I am building my static library using the object libraries as shown using CMake 3.1.3.

I have

ADD_SUBDIRECTORY(A)
ADD_SUBDIRECTORY(B)
....
ADD_LIBRARY(mylib STATIC ${SOURCES}
                  $<TARGET_OBJECTS:A> 
                  $<TARGET_OBJECTS:B> )
SET_TARGET_PROPERTIES(mylib PROPERTIES COMPILE_PDB_NAME mylib COMPILE_PDB_OUTPUT_DIR ${CMAKE_BINARY_DIR})

Now, my problem is A generates vc120.pdb in A's CMake subdirectory. B generates its own vc120.pdb in B's CMake subdirectory. And, mylib generates mylib.pdb in the main binary cmake folder.

I only want one static library and one pdb file. I just want mylib and mylib.pdb.

How can I merge all the vc120.pdbs into mylib.pdb or ideally just generate only one pdb file?

回答1:

This is not a direct answer to your question, but an alternate solution you may want to consider.

With static libraries, you are probably better off using /Z7 for the generation of debugging information. When using /Z7 the compiler does not produce a .PDB file, but embeds debugging info directly into the generated object files.

When these object files are then linked as a static library, lib.exe will copy the debugging info from all object files to the resulting .lib file. There is not need to distribute a .pdb file with the .lib file.

Unlike link.exe, which is used by CMake to produce a DLL or and EXE, lib.exe does not have an option to output a .PDB file.

Via CMake you can set the required options in the following way. For the object library use:

add_library(A OBJECT lib2.cpp)
set_target_properties(A PROPERTIES COMPILE_OPTIONS "/Z7")

To produce the final static library, use:

add_library(mylib STATIC main.cpp $<TARGET_OBJECTS:A> $<TARGET_OBJECTS:B> )
set_target_properties(mylib PROPERTIES COMPILE_OPTIONS "/Z7")


回答2:

I have managed to contact folks at Kitware (CMake owner).

They have said,

"Set the COMPILE_PDB_* properties of A, B, and mylib all to point to the same place. Object libraries are built independently and do not know what will consume them (or if multiple targets consume them), so they need to be configured individually."

So, inside A & B, do

add_library(A OBJECT a.c)
set_target_properties(A PROPERTIES
    COMPILE_PDB_NAME "mylib"
    COMPILE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
)