使用CMake 2.8无法完成交叉编译,附构建结构及调用脚本
Let’s walk through the most likely issues with your setup—older CMake versions like 2.8 have plenty of quirks, and cross-compilation often falls apart due to small path or configuration mistakes. Here’s what to check step by step:
1. Fix the Toolchain File Path
Your current -DCMAKE_TOOLCHAIN_FILE=../toolchain.cmake is pointing to the wrong location. Let’s map your directory structure:
- When you
cd libyang-0.14.78, you’re in/blah/3rdparty/yang/libyang-0.14.78 ../would take you to/blah/3rdparty/yang, but yourtoolchain.cmakelives in/blah/3rdparty
You need to use two levels of parent directories to reach it:
-DCMAKE_TOOLCHAIN_FILE=../../toolchain.cmake
2. Verify Your Toolchain File is Correct
CMake 2.8 relies heavily on a properly configured toolchain file for cross-compilation. Make sure yours includes these critical variables (adjust paths to match your cross-compiler):
# Set target system (e.g., Linux for embedded targets) SET(CMAKE_SYSTEM_NAME Linux) # Path to your cross-compiler binaries SET(CMAKE_C_COMPILER /path/to/your/cross-gcc) SET(CMAKE_CXX_COMPILER /path/to/your/cross-g++) # Optional but recommended: Sysroot for target libraries/headers SET(CMAKE_SYSROOT /path/to/your/target-sysroot) # Tell CMake to only search the target sysroot for libraries/headers SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
If any of these are missing, CMake will default to your host system’s compiler, which is why cross-compilation fails.
3. Ensure You’re Using a Cross-Compiled PCRE Library
The PCRE paths you specified (/blah/lib/libpcre.so and /blah/include) look like host system files. Cross-compiling requires linking against libraries built for your target architecture, not your development machine’s.
Replace these paths with the location of your cross-compiled PCRE library and headers, e.g.:
-DPCRE_LIBRARY=/blah/cross-target/lib/libpcre.so -DPCRE_INCLUDE_DIR=/blah/cross-target/include
4. Fix the Makefile’s Command Chaining
Using ; after cd means the subsequent cmake command runs in your original working directory (not libyang-0.14.78) if the cd fails silently. Switch to && to ensure the chain only proceeds if the cd succeeds:
prep: cd libyang-0.14.78 && \ cmake -DCMAKE_TOOLCHAIN_FILE=../../toolchain.cmake \ -DENABLE_BUILD_TESTS=OFF \ -DENABLE_VALGRIND_TESTS=OFF \ -DPCRE_LIBRARY=/blah/cross-target/lib/libpcre.so \ -DPCRE_INCLUDE_DIR=/blah/cross-target/include \ -DCMAKE_BUILD_TYPE=Release
5. Clear CMake Cache Residues
Older CMake versions (like 2.8) don’t always overwrite cached values properly. If you’ve run cmake before, delete the cached files in libyang-0.14.78 to start fresh:
rm -rf libyang-0.14.78/CMakeCache.txt libyang-0.14.78/CMakeFiles
6. Check libyang 0.14.78’s CMake Compatibility
Double-check that libyang 0.14.78’s CMakeLists.txt supports CMake 2.8. Some modern CMake commands (like target_link_libraries with keyword arguments) weren’t available in 2.8. If you see errors about unknown commands, you may need to either:
- Patch the libyang CMakeLists.txt to use 2.8-compatible syntax
- Upgrade to a newer CMake version (if your environment allows it)
Start with the path and toolchain fixes first—those are the most common culprits for this kind of failure.
内容的提问来源于stack exchange,提问作者codenamezero

