[CMake] How to get list of generated object-files of OBJECT-library?

Deniz Bahadir deniz.bahadir at benocs.com
Mon Jul 17 09:52:12 EDT 2017


Hi list,


I have a question regarding "add_custom_command" and OBJECT-libraries:

My CMakeLists.txt contains the following (simplified) targets:

```
set ( SOURCE_FILES
     # ... several C++-source files
)
add_library( OBJECT ${PROJECT_NAME}_OBJECTS
     ${SOURCE_FILES}
)
add_library( ${PROJECT_NAME}
    $<TARGET_OBJECTS:${PROJECT_NAME}_OBJECTS>
)
```

This works just fine. However, now I want to add an additional 
build-step after creation of the object-files and before linking the 
shared library. (In particular, I want to compress the debug-symbols in 
the object-files. But that should be irrelevant for my question.)

My first attempt was to add the following between the two add_library 
commands:
```
add_custom_command( TARGET ${PROJECT_NAME}_OBJECTS POST_BUILD
     COMMAND objcopy --compress-debug-sections 
$<TARGET_OBJECTS:${PROJECT_NAME}_OBJECT>
)
```
But that results in the following error:
```
(add_custom_command):
   Target "MyProject_OBJECTS" is an OBJECT library that may not
   have PRE_BUILD, PRE_LINK, or POST_BUILD commands.
```

So I tried instead to add the following between the two add_library 
commands:
```
add_custom_command( TARGET ${PROJECT_NAME} PRE_LINK
     COMMAND objcopy --compress-debug-sections 
$<TARGET_OBJECTS:${PROJECT_NAME}_OBJECT>
)
```
This then fails when evaluating the generator-expression:
```
(add_custom_command):
   Error evaluating generator expression:

     $<TARGET_OBJECTS:MyProject_OBJECTS>

   The evaluation of the TARGET_OBJECTS generator expression is only 
suitable
   for consumption by CMake.  It is not suitable for writing out elsewhere.
```

So I am currently out of ideas (which do not make the CMakeLists.txt 
file completely unmaintainable). Therefore my question is:

How can I retrieve the list of generated object-files so that I can pass 
it to another program that should be run as additional build-step before 
linking?

IMHO, the most comfortable (and syntactically cleanest) way would be my 
first attempt. But that is probably more a feature-request than a simple 
question.


Thanks for your help.
Deniz Bahadir



More information about the CMake mailing list