使用CMake

时间:2016-06-17 11:14:53

标签: ios cmake static-libraries

我尝试使用CMake创建项目json11的静态库,以便在iOS项目中使用。

我在这里创建了一个示例项目:https://github.com/4brunu/json11CMakeiOS

构建json11目标时,它工作正常,但json11_test目标返回以下错误:

=== BUILD TARGET json11_test OF PROJECT example_all WITH CONFIGURATION Debug ===

Check dependencies
target specifies product type 'com.apple.product-type.tool', but there's no such product type for the 'iphoneos' platform

** BUILD FAILED **


The following build commands failed:
    Check dependencies
(1 failure)

原来的json11 CMakeLists.txt是这样的:

cmake_minimum_required(VERSION 3.2)
project(json11 VERSION 1.0.0 LANGUAGES CXX)

enable_testing()

add_library(json11 json11.cpp)
target_include_directories(json11 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_options(json11
  PUBLIC -std=c++11
  PRIVATE -fno-rtti -fno-exceptions -Wall -Wextra -Werror)
configure_file("json11.pc.in" "json11.pc" @ONLY)

add_executable(json11_test test.cpp)
target_link_libraries(json11_test json11)

install(TARGETS json11 DESTINATION lib)
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/json11.hpp" DESTINATION include)
install(FILES "${CMAKE_BINARY_DIR}/json11.pc" DESTINATION lib/pkgconfig)

经过一些测试后,我修改了CMakeLists.txt并且它有效,但我试图找出错误。

cmake_minimum_required(VERSION 3.2)
project(json11 VERSION 1.0.0 LANGUAGES CXX)

enable_testing()

add_library(json11 json11.cpp)
target_include_directories(json11 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_options(json11
  PUBLIC -std=c++11
  PRIVATE -fno-rtti -fno-exceptions -Wall -Wextra -Werror)
configure_file("json11.pc.in" "json11.pc" @ONLY)

# Commented this two lines
# add_executable(json11_test test.cpp)
# target_link_libraries(json11_test json11)

install(TARGETS json11 DESTINATION lib)
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/json11.hpp" DESTINATION include)
install(FILES "${CMAKE_BINARY_DIR}/json11.pc" DESTINATION lib/pkgconfig)

不确定问题是否在CMake配置1 2iOS toolchainjson11 CMake或其他任何内容中。

感谢您的帮助。

修改

我没有找到解决方案,而是找到了解决办法,使测试可选。

https://github.com/4brunu/json11CMakeiOS/commit/972f00b646057a513d14a90b874f9f398fcff873 https://github.com/dropbox/json11/pull/66

1 个答案:

答案 0 :(得分:0)

问题是库附带的测试目标。在C和C ++中,您通常会有一个运行测试套件的专用命令行可执行文件。在OSX上,有一个名为命令行工具的目标类型。 Xcode引用具有反向DNS标识符的目标类型,在本例中为com.apple.product-type.tool。在iOS上,您没有命令行工具的目标类型,因为没有 public 命令行可以从中运行可执行文件。

话虽如此,您已经自己找到了解决方案:如果您想为iOS构建,请在CMake中停用测试目标生成。