简体   繁体   中英

Compile and link two files using cmake?

I have two files in my project.
One is a ".c" file, one is a ".asm" file. First, I compile both the files into two ".o" object files and then link together using a custom linker script.
Currently, I am using a simple bash script with manual commands to build the project, but as the project size is expected to increase, I was thinking of moving the project to using cmake as its primary build system.

I have searched documentation of cmake and googled for a lot of time, although I have shortlisted some of the variables in cmake which might prove to be useful in this case, I am unable to find any sample explaining how to achieve this.

Can anybody provide me with a brief sample code on how should I go about achieving this ?

You asked for an example? Here's one from the cmake wiki

set(mySrcs foo.c bar.c)

set(can_use_assembler FALSE)

# test wether it is a x86 machine and as/gas is available
if("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "i.86")
   enable_language(ASM-ATT)
   if(CMAKE_ASM-ATT_COMPILER_WORKS)
      set(can_use_assembler TRUE)
      set(mySrcs ${mySrcs} codec_i86.s)
   endif(CMAKE_ASM-ATT_COMPILER_WORKS)
endif("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "i.86")

# no assembler found
if(not can_use_assembler)
   set(mySrcs ${mySrcs} codec_generic.c)
endif(not can_use_assembler)

add_executable(player ${mySrcs})

Because of the way that CMake works, assembling and linking are grouped together in the same step (from the programmer's perspective at least). So add_executable automatically handles everything for you.

If you want to include a linker script, that gets more difficult with CMake. The best place to look into it is over here . Since the examples and instructions are too long to copy out in the SO answer box, I'll leave the reading to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM