0
我有我的CMakeList.txt中的2個庫和1個可執行文件。我希望鏈接到可執行文件的所有內容。如何將庫鏈接到cmake中的可執行文件?
cmake_minimum_required(VERSION 2.8)
# Mark the language as C so that CMake doesn't try to test the C++
# cross-compiler's ability to compile a simple program because that will fail
project(jsos C ASM)
set(CMAKE_EXECUTABLE_OUTPUT_PATH "./build/")
# We had to adjust the CMAKE_C_FLAGS variable in the toolchain file to make sure
# the compiler would work with CMake's simple program compilation test. So unset
# it explicitly before re-setting it correctly for our system
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostartfiles")
# Set the linker flags so that we use our "custom" linker script
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-T,${PROJECT_SOURCE_DIR}/etc/linker.ld")
add_library(duktape STATIC
src/libs/duktape/duktape.c
)
add_library(fdlibm STATIC
src/libs/fdlibm/e_acos.c
src/libs/fdlibm/e_acosh.c
src/libs/fdlibm/e_asin.c
MORE FILES
)
add_executable(kernel
src/start.S
src/kernel.c
src/cstartup.c
src/cstubs.c
src/rpi-gpio.c
src/rpi-interrupts.c
src/rpi-armtimer.c
src/rpi-systimer.c
)
add_dependencies(kernel fdlibm duktape)
target_link_libraries(kernel fdlibm duktape)
add_custom_command(
TARGET kernel POST_BUILD
COMMAND ${CMAKE_OBJCOPY} ./kernel${CMAKE_EXECUTABLE_SUFFIX} -O binary ./kernel.img
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Convert the ELF output file to a binary image"
)
在那一刻,我鏈接這些我一起得到像一堆錯誤:
[100%] Linking C executable kernel
libduktape.a(duktape.c.obj): In function `duk_double_trunc_towards_zero':
src/libs/duktape/duktape.c:12102: undefined reference to `fabs'
src/libs/duktape/duktape.c:12102: undefined reference to `floor'
但fabs
和floor
是fdlibm
。 duk_double_trunc_towards_zero
在duktape
庫中,因此似乎鏈接OK。我究竟做錯了什麼?
嘗試在'target_link_libraries'調用中交換庫的順序。 – arrowd
@arrowd作出回答,以便接受它。這是對的 – Justin808