immutablestate
immutablestate

Reputation: 297

CMake: linking assembly against libc on Linux

I'm trying to learn assembly programming at the moment, and I'm using CMake to build my projects and exercises.

The book I'm following tells me to link one of the example programs with the C standard library using this command line (Programming from the Ground Up, Chapter 8):

ld printf-example.o -o printf-example -lc -dynamic-linker /lib/ld-linux.so.2

But I'm not sure how to replicate this behaviour from within CMake.

At the moment, my CMake file looks like this:

project(ch8)

enable_language(ASM-ATT)

include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/include)

add_executable(printf-example printf-example.s)
target_link_libraries(printf-example c)

If I leave off the target_link_libraries line, make fails with 'undefined reference' errors to the libc functions referenced in printf-example.s.

If I include the line, make succeeds, but when I try to run the program I get the error

bash: ./printf-example: No such file or directory

file gives me this output:

printf-example: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), \
dynamically linked (uses shared libs), not stripped

uname -m gives me i686, so I don't think I'm linking against the libc for a different architecture.

Does anyone know how to link assembly programs against the C standard library in CMake?

Upvotes: 1

Views: 1251

Answers (1)

immutablestate
immutablestate

Reputation: 297

At the moment I can get it to work with this statement, but any improvements upon it would be well received.

set_target_properties(
    printf-example
    PROPERTIES
    LINK_FLAGS "-lc -dynamic-linker /lib/ld-linux.so.2"
)

Upvotes: 1

Related Questions