[CMake] Building Libraries with C and ASM sources

Brad King brad.king at kitware.com
Tue Mar 15 11:29:51 EST 2005


Joe Cotellese wrote:
> Hello,
> 
> I'm trying to build a library from C and ASM sources. I found an earlier 
> post describing how this is done but I'm afraid I'm too dense to get it 
> working.
> 
> Here is what I have so far.
> 
> SET (MY_SRCS
> a.cpp
> b.cpp
> c.cpp
> d.cpp
> e.cpp
> f.c
> )
> 
> SET (MY_ASM_SRCS
> g.asm
> h.asm
> )
> 
> #This bit of code loops through the assembly files and calls
> #nasm.
> FOREACH (SOURCE ${MY_ASM_SRCS})
>    GET_FILENAME_COMPONENT (FILE_BASE ${SOURCE} NAME_WE)
>    ADD_CUSTOM_COMMAND (SOURCE ${SOURCE}
>    OUTPUT ${FILE_BASE}.o
>    ARGS "-felf" ${SOURCE}
>    COMMAND "nasm"
>    )
> ENDFOREACH(SOURCE)
> 
> ADD_LIBRARY (mylib ${MY_SRCS})
> 
> Now, the question is, how do I add the objects to the ADD_LIBRARY line? 
> I tried adding g.o and h.o as such:
> ADD_LIBRARY (mylib ${MY_SRCS} g.o h.o)

Specify the full path to the .o files both for the OUTPUT of the custom 
command an in the ADD_LIBRARY call:

FOREACH(src ${MY_ASM_SRCS})
   GET_FILENAME_COMPONENT(FILE_BASE ${src} NAME_WE)
   SET(SRC ${CMAKE_CURRENT_SOURCE_DIR}/${src})
   SET(OBJ ${CMAKE_CURRENT_BINARY_DIR}/${FILE_BASE}.o)
   ADD_CUSTOM_COMMAND(OUTPUTS ${OBJ}
                      MAIN_DEPENDENCY ${SRC}
                      COMMAND nasm
                      ARGS -felf ${SRC})
   SET(MY_ASM_OBJS ${MY_ASM_OBJS} ${OBJ})
ENDFOREACH(src)

ADD_LIBRARY(mylib ${MY_SRCS} ${MY_ASM_OBJS})

You might want to check if nasm has a "-o" option to specify the name of 
the object file on the command line.

-Brad


More information about the CMake mailing list