[CMake] making executible in mixed C/fortran project

Michael Hertling mhertling at online.de
Fri Nov 12 16:58:51 EST 2010


On 11/12/2010 05:37 PM, luxInteg wrote:
> On Friday 12 November 2010 13:17:50 Michael Hertling wrote:
>> On 11/12/2010 02:46 PM, luxInteg wrote:
>>> Greetings,
>>>
>>> I am learning cmake
>>>
>>> I am building an executable  which needs to   first compile a C program 
>>> and then recompiling the object file   with another file in fortran.
>>>
>>>
>>> Here is  an excerpt from a unix makefile I am attempt to port to cmake as
>>> part of my cmake turotial.
>>>
>>> fileC.o: fileC.c
>>>
>>> 	$(C) -DDINT -c fileC.c
>>>
>>> fileD:  fileD.f fileC.o ../lib/libF.a
>>>
>>> 	$(F77) $(F77FLAGS) -o fileD fileD.f fileC.o    ../lib/libF.a $(F77LIB)
>>>
>>> I  think I have learnt enough cmake to do most of the above  EXCEPT I am
>>> unsure how on compile an object file  (in this case  fileC.o
>>> with a fortran compiler (if I have translated the makefile correctly)
>>>
>>>  Does one  rename   the file or whatever?
>>>
>>> Guidance on how to do this would be appreciated
>>
>> You might try
>>
>> ADD_LIBRARY(C STATIC fileC.c)
>>
>> in junction with
>>
>> TARGET_LINK_LIBRARIES(fileD C ...)
> 
> ----------------
> It would not go in the loop 
> 
> add_executable(fileD
> fileD.f 
> ADD_LIBRARY(fileC STATIC fileC.c )
> ${fileC}  )

Which loop are you talking about, and which value does the "fileC"
variable has here? Moreover, the ADD_LIBRARY() command must not
appear among the source files of ADD_EXECUTABLE(), of course.

> so I tried  it outside like so:-
> 
> SET_SOURCE_FILES_PROPERTIES( fileC.c  PROPERTIES  COMPILE_DEFINITIONS DINT )
> ADD_LIBRARY(fileC STATIC fileC.c )
> add_executable(fileD
> fileD.f 
> ${fileC}  )
> 
> target_link_libraries(fileD   $(F77LIB) )

That's better, but you need to mention the fileC library target in
TARGET_LINK_LIBRARIES() for this to work, and again: What's the value
of the "fileC" variable here? Note that you've an equally named target.

If you don't really intent to re-use fileC.c, but just want to mix it
with the Fortran sources of fileD, you can do so, i.e. you might say

ADD_EXECUTABLE(fileD fileD.f fileC.c)

but when mixing several languages read about the LINKER_LANGUAGE
target property as well as the CMAKE_<LANG>_LINKER_PREFERENCE
and CMAKE_<LANG>_LINKER_PREFERENCE_PROPAGATES variables.

> -----------but  make ends like so:-
> CMakeFiles/filedD.dir/filedD.f.o: In function `MAIN__':
> filedD.f:(.text+0x2f9): undefined reference to `Ddefaults_'
> filedD.f:(.text+0x32b): undefined reference to `Dorder_'
> filedD.f:(.text+0x33a): undefined reference to `Dinfo_'
> collect2: ld returned 1 exit status

Usually, that means there are missing sources in ADD_EXECUTABLE() or
missing libraries in TARGET_LINK_LIBRARIES(); perhaps, you can post
a complete but minimal example as a demonstration of this issue.

> oher suggestions welcomed
>> since you shouldn't directly refer to object files with CMake.

Regards,

Michael


More information about the CMake mailing list