如何使用 cmake+clang 从 windows 编译到 raspberry pi 4?

时间:2021-02-01 09:11:58

标签: c++ windows cmake clang

我试图用clang将简单的helloworld从windows机器编译到raspberry pi机器。但它给了我错误。

从 llvm.org 下载的 Clang。安装到 C:/Program Files/LLVM。

CMakeLists.txt 文件

cmake_minimum_required(VERSION 3.8.0)
project(test)

add_executable(${PROJECT_NAME} 
    main.cpp
)

clang_arm.cmake 文件

cmake_minimum_required(VERSION 3.10)

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR ARM)

set(CLANG_TARGET_TRIPLE arm-none-eabi)

set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_ASM_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET ${CLANG_TARGET_TRIPLE})
set(CMAKE_CXX_COMPILER_TARGET ${CLANG_TARGET_TRIPLE})
set(CMAKE_ASM_COMPILER_TARGET ${CLANG_TARGET_TRIPLE})

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

ma​​in.cpp 文件

#include <iostream>

int main()
{
    std::cout << "hello world" << std::endl;
    system("pause");
    return 0;
}

build.cmd 文件

mkdir build
cd build
cmake .. -G Ninja -DCMAKE_TOOLCHAIN_FILE=clang_arm.cmake
ninja

当我尝试执行 build.cmd 文件时,它输出错误:

C:\dev\test>mkdir build

C:\dev\test>cd build

C:\dev\test\build>cmake .. -G Ninja -DCMAKE_TOOLCHAIN_FILE=clang_arm.cmake
-- The C compiler identification is Clang 11.0.0
-- The CXX compiler identification is Clang 11.0.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/Program Files/LLVM/bin/clang.exe
-- Check for working C compiler: C:/Program Files/LLVM/bin/clang.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.19/Modules/CMakeTestCCompiler.cmake:66 (message):
  The C compiler

    "C:/Program Files/LLVM/bin/clang.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/dev/test/build/CMakeFiles/CMakeTmp

    Run Build Command(s):C:/PROGRA~1/Ninja/ninja.exe cmTC_0203b && [1/2] Building C object CMakeFiles/cmTC_0203b.dir/testCCompiler.c.obj
    [2/2] Linking C executable cmTC_0203b
    FAILED: cmTC_0203b
    cmd.exe /C "cd . && C:\PROGRA~1\LLVM\bin\clang.exe --target=arm-none-eabi   CMakeFiles/cmTC_0203b.dir/testCCompiler.c.obj -o cmTC_0203b   && cd ."
    ld.lld: error: unable to find library -lc
    ld.lld: error: unable to find library -lm
    ld.lld: error: unable to find library -lclang_rt.builtins-arm
    clang: error: ld.lld command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:2 (project)


-- Configuring incomplete, errors occurred!
See also "C:/dev/test/build/CMakeFiles/CMakeOutput.log".
See also "C:/dev/test/build/CMakeFiles/CMakeError.log".

C:\dev\test\build>ninja
ninja: error: loading 'build.ninja': ═х єфрхЄё  эрщЄш єърчрээ√щ Їрщы.

1 个答案:

答案 0 :(得分:0)

正如链接器的输出所提到的

ld.lld: error: unable to find library -lc
ld.lld: error: unable to find library -lm
ld.lld: error: unable to find library -lclang_rt.builtins-arm

它找不到 libm、libc 和 libclang_rt.buildins-arm。您必须使用 -L 提供库路径。您还可以在文件 clang_arm.cmake 中使用 CMAKE_SYSROOT 告诉 CMAKE 默认情况下它应该查找系统库的位置

set(CMAKE_SYSROOT /your/path/with/std_libraries)