[CMake] Add pre-existing object file to linker line?

Bill Hoffman bill.hoffman at kitware.com
Sun May 27 13:37:02 EDT 2007


Trevor Kellaway wrote:
> Bill,
>
>   
>>> ADD_EXECUTABLE(myapp mysrc1.c mysrc2. myexisiting.o)
>>>
>>> If I perform the above CMake complains it doesn't know how to build 
>>> myexisiting.o.
>>>
>>>       
>> What version of CMake are you using? The above should work.
>>     
>
> Sorry, my fault, you are right it does work, I was stupidly not
> specifying the full (or relative path to the file, brain fade thinking
> it might be found in the link path but obviously not). Thanks.
>
>
> Previously we discussed using "GET_TARGET_PROPERTY (_obj_list ${target}
> OBJECT_FILES)" to determine the absolute object file path for a source
> file. This works, but I'm in catch 22 situation, as I can only determine
> a source file's object file once it has been added to a library or
> executable, and by then it is too late for me to use this information to
> add other source file (.lint) rules to the library. (Unless you can add
> extra source file dependencies to a previous ADD_LIBRARY?)
>
> Allow me to take a step back, what I'm trying to do is to get Lint to
> run if a source file or its dependencies have changed. In my straight
> makefile world I'd just add an extra command line to the .c.obj
> inference rule. I can't see how I can do this in CMake, under Unix I
> could use the shell "&&" to run two commands for the compile, but under
> Windows this is a non-starter.
>
> Currently I'm creating a phoney target "filename.lint" and making this
> depend on the object file, but having to predict the object output
> directory, which is very fragile, and breaks if a CMakeLists.txt
> references a source file out of the tree (CMake works but produces a
> huge path under the binary directory that can't be easily predicted).
>
> What do you suggest? You're help would be gratefully appreciated.
>   
You want to create your own rules for CMAKE_(LANG)_COMPILE_OBJECT:

SET(CMAKE_C_COMPILE_OBJECT
    "<CMAKE_C_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO} 
<FLAGS> /Fo<OBJECT> /Fd<TARGET_PDB>  -c <SOURCE>${CMAKE_END_TEMP_FILE}")

Each new line in this is a separate command.   You should just be able 
to append the lint stuff to end end of the existing rule variable.

Something like this:

set(CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT\}
lint <OBJECT>")

-Bill




More information about the CMake mailing list