2017-05-27 84 views
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' 

fabsfloorfdlibmduk_double_trunc_towards_zeroduktape庫中,因此似乎鏈接OK。我究竟做錯了什麼?

+2

嘗試在'target_link_libraries'調用中交換庫的順序。 – arrowd

+0

@arrowd作出回答,以便接受它。這是對的 – Justin808

回答

1

在你的聲明:

target_link_libraries(kernel fdlibm duktape) 

要解決從提供庫的有序列表中搜索(fdlibm duktape在這種情況下)這樣的外部referece符號:

  • 從開始圖書館被鏈接;
  • 在它右側的庫上搜索符號;

在你的情況,解決的duktape外部符號(其中一些是fdlibm)時,fdlibm甚至沒有用於該搜索,並duktape需要的符號都沒有發現。只需在之後放置fdlibm ,duktape將讓符號被找到。

例如,如果您取決於duktape定義的一些符號,而且周圍的其他方法有一個fdlibm,你應該使用:

target_link_libraries(kernel fdlibm duktape fdlibm) 

使符號總是解決。

相關問題