使用cmake交叉编译多个库和应用程序

时间:2018-07-05 19:44:52

标签: c cmake

在尝试交叉编译现有的大型代码库时,我遇到了cmake的问题。我们当前的构建系统使用了一个专有的IDE,该IDE会导致大量问题-因此,我们正在努力解决这一问题。

我已将其精炼为最简单的示例。

注意:当我在cmake中不使用工具链选项时,所有这些都可以使用。如果仅使用本机工具链,我就可以编译并运行它。

我目前不怀疑cmake的工具链文件有问题。我用它来编译OpenCV -并有一些黑客攻击OpenCV。

尝试交叉编译时遇到的错误是:

-- The C compiler identification is GNU 4.8.3
-- The CXX compiler identification is GNU 4.8.3
-- Check for working C compiler: /opt/qnx650/host/linux/x86/usr/bin/ntox86-gcc
-- Check for working C compiler: /opt/qnx650/host/linux/x86/usr/bin/ntox86-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /opt/qnx650/host/linux/x86/usr/bin/ntox86-g++
-- Check for working CXX compiler: /opt/qnx650/host/linux/x86/usr/bin/ntox86-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/MY_USER/Desktop/test_app/build-test_app
Scanning dependencies of target liba
[ 25%] Building C object libs/liba/CMakeFiles/liba.dir/liba.c.o
Linking C shared library libliba.so
[ 25%] Built target liba
Scanning dependencies of target libb
[ 50%] Building C object libs/libb/CMakeFiles/libb.dir/libb.c.o
make[2]: *** No rule to make target `libs/liba/liba.a', needed by `libs/libb/liblibb.so.1.0.0'.  Stop.
make[1]: *** [libs/libb/CMakeFiles/libb.dir/all] Error 2
make: *** [all] Error 2

这就像是在寻找该库的静态版本,但我已将其配置为在调用cmake时使用共享对象:

cmake -DBUILD_SHARED_LIBS=1 ..

我已将所有文件包含在下面的测试项目中。在尝试编译更大的项目时,我遇到了同样的问题。

项目结构:

test_app
├── app
│   ├── appa
│   │   ├── appa.c
│   │   └── CMakeLists.txt
│   └── CMakeLists.txt
├── CMakeLists.txt
├── include
│   ├── liba
│   │   └── liba.h
│   ├── libb
│   │   └── libb.h
│   └── libc
│       └── libc.h
└── libs
    ├── CMakeLists.txt
    ├── liba
    │   ├── CMakeLists.txt
    │   └── liba.c
    ├── libb
    │   ├── CMakeLists.txt
    │   └── libb.c
    └── libc
        ├── CMakeLists.txt
        └── libc.c

顶级CMakeLists.txt

project(test)

cmake_minimum_required(VERSION 2.8)

set(CMAKE_INSTALL_PREFIX "/opt/test")
set(INSTALL_PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_EXECUTE)

set(TEST_VERSION 1.0.0)
set(TEST_MAJOR_VERSION 1)

set(TEST_GLOBAL_INCLUDES
  $<BUILD_INTERFACE:${CROSS_TARGET}/usr/pkg/include>
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

set(TEST_GLOBAL_LIB_PATHS
  $<BUILD_INTERFACE:${CROSS_TARGET}/usr/pkg/include>
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

add_subdirectory(libs)
add_subdirectory(app)

app / CMakeLists.txt

add_subdirectory(appa)

app / appa / CMakeLists.txt

project(appa)

add_executable(appa appa.c)

target_include_directories(appa PUBLIC ${TEST_GLOBAL_INCLUDES})
target_link_libraries(appa libc)

# Set version information
set_target_properties(appa PROPERTIES
    VERSION ${TEST_VERSION}
)

# set install destination and permissions
install(
    TARGETS appa
    RUNTIME DESTINATION lib
    PERMISSIONS ${INSTALL_PERMISSIONS}
)

app / appa / appa.c

#include <stdlib.h>
#include <stdio.h>

#include <libc/libc.h>

int main(int argc, char **argv)
{
    printf("Hello world: %d\n", libc_function(2));
    return EXIT_SUCCESS;
}

include / liba / liba.h

#pragma once

int liba_function(int a);

include / libb / libb.h

#pragma once

int libb_function(int a);

include / libc / libc.h

#pragma once

int libc_function(int a);

libs / CMakeLists.txt

add_subdirectory(liba)
add_subdirectory(libb)
add_subdirectory(libc)

libs / liba / CMakeLists.txt

project(libliba)

add_library(liba liba.c)

target_include_directories(liba PUBLIC ${TEST_GLOBAL_INCLUDES})

# Set version information
set_target_properties(liba PROPERTIES
    VERSION ${TEST_VERSION}
    SOVERSION ${TEST_MAJOR_VERSION}
)

# set install destination and permissions
install(
    TARGETS liba
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION lib
    PERMISSIONS ${INSTALL_PERMISSIONS}
)

libs / liba / liba.c

#include <liba/liba.h>

int liba_function(int a)
{
    return a * a;
}

libs / libb / CMakeLists.txt

project(liblibb)

add_library(libb libb.c)

target_include_directories(liba PUBLIC ${TEST_GLOBAL_INCLUDES})

# Set version information
set_target_properties(libb PROPERTIES
    VERSION ${TEST_VERSION}
    SOVERSION ${TEST_MAJOR_VERSION}
)

target_link_libraries(libb liba)

# set install destination and permissions
install(
    TARGETS libb
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION lib
    PERMISSIONS ${INSTALL_PERMISSIONS}
)

libs / libb / libb.c

#include <liba/liba.h>
#include <libb/libb.h>

int libb_function(int a)
{
    return liba_function(a) * liba_function(a);
}

libs / libc / CMakeLists.txt

project(liblibc)

add_library(libc libc.c)

target_include_directories(libc PUBLIC ${TEST_GLOBAL_INCLUDES})

# Set version information
set_target_properties(libc PROPERTIES
    VERSION ${TEST_VERSION}
    SOVERSION ${TEST_MAJOR_VERSION}
)

target_link_libraries(libc libb)

# set install destination and permissions
install(
    TARGETS libc
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION lib
    PERMISSIONS ${INSTALL_PERMISSIONS}
)

libs / libc / libc.c

#include <libb/libb.h>
#include <libc/libc.h>

int libc_function(int a)
{
    return libb_function(a) * libb_function(a);
}

0 个答案:

没有答案