Why is CMake apparently referring to host system files while cross-compiling a Net-SNMP agent despite a proper toolchain file is being used?

依然范特西╮ 提交于 2019-12-04 22:23:42

You need to set CMAKE_SYSROOT variable for refer to "here is the target environment located".

Unlike to CMAKE_FIND_ROOT_PATH variable, which is used only in find_* commands, CMAKE_SYSROOT is used also as a hint to the compiler (--sysroot option), so compiler will pick correct includes.


In case of cross-compiling, variable CMAKE_FIND_ROOT_PATH is used for provide additional search prefixes for find_* commands. CMAKE_SYSROOT is used as a prefix automatically.

FINAL SOLUTION:

Like explained on the question UPDATE, @Tsyvarev's answer fixed the linking part by providing the --sysroot switch to the compiler pointing to the root filesystem for ARM, but the problem of finding a stubs-32.h from the host system isn't fixed by that. Although it was part of the solution, it is important to note the primary observation had another cause: the include -I switches were being used directly as CFLAGS to the compiler, making it effectively look at the host system, since they where not prefixed with the root filesystem path for the ARM board (remember the flags were output directly by the net-snmp-config script, which was reporting flags for the native ARM build, so with "normal" paths instead). To fix that I used a CMake string command to remove all the -I switches from the NETSNMPCFLAGS variable:

# Gets compiling flags and libs linked to Net-SNMP
execute_process(COMMAND "${NETSNMPCONFIG}" "--base-cflags" OUTPUT_VARIABLE NETSNMPCFLAGS)
execute_process(COMMAND "${NETSNMPCONFIG}" "--agent-libs" OUTPUT_VARIABLE NETSNMPLIBS)
# removes the include dir switches "-I" from the NETSNMPCFLAGS, since we don't want
# the compiler to include paths relative to the host system in the compilation
string(REGEX REPLACE "-I[a-zA-Z0-9/]*" "" NETSNMPCFLAGS ${NETSNMPCFLAGS})

set(STRICT_FLAGS "-Wall -Wstrict-prototypes")
set(CFLAGS "-I. ${STRICT_FLAGS} ${NETSNMPCFLAGS}")

and put the root filesystem in an include_directories directive:

# Sets the flags to be used for compiling and linking the executable
set_source_files_properties(${SRCS} COMPILE_FLAGS ${CFLAGS})
include_directories(${CMAKE_FIND_ROOT_PATH}/usr/include)
add_executable(${CMAKE_PROJECT_NAME} ${SRCS})
target_link_libraries(${CMAKE_PROJECT_NAME} ${NETSNMPAGENT} ${NETSNMPMIBS} ${NETSNMP})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!