虽然编译成功了,但是还是有一些不妥的地方,参考一下就行了。
比如库的版本选择就有待商榷,我这里不是按照项目作者的要求严格用对应的版本编译的,这里也可以注意一下。
编译依赖库asio
下载地址, 更正一下,我其实用的是1.13 GitHub - chriskohlhoff/asio at asio-1-12-0https://github.com/chriskohlhoff/asio/tree/asio-1-12-0https://github.com/chriskohlhoff/asio/tree/asio-1-12-0
编译命令
./autogen.sh
./configure --host=arm-buildroot-linux-gnueabihf --prefix=/home/book/test/asio-asio-1-13-0/asio/install
make
make install
编译 openssl
下载地址 GitHub - openssl/openssl at OpenSSL_1_1_1lhttps://github.com/openssl/openssl/tree/OpenSSL_1_1_1lhttps://github.com/openssl/openssl/tree/OpenSSL_1_1_1l
编译:
./config no-asm shared no-async --prefix=${inst_dir} --cross-complie-prefix=arm-buildroot-linux-gnueabihf-
删除makefile 中的 -m64
make
make install
编译我们的主角 asio-dtls
下载地址:
GitHub - openssl/openssl at OpenSSL_1_1_1lhttps://github.com/openssl/openssl/tree/OpenSSL_1_1_1lhttps://github.com/sdamm/asio_dtls/tree/asio_dtls_1.12.0.0
要修改CMakeLists.txt 才能编译过:
贴出来:
cmake_minimum_required (VERSION 3.2)
project(asio_dtls)
set(OPENSSL_ROOT_DIR /home/book/test/openssl-OpenSSL_1_1_1l/install)
include_directories(/home/book/test/asio-asio-1-13-0/asio/install/include)
find_program(
CLANG_TIDY_EXE
NAMES "clang-tidy"
DOC "Path to clang-tidy executable"
)
if(NOT CLANG_TIDY_EXE)
message(STATUS "clang-tidy not found.")
else()
message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
set(DO_CLANG_TIDY "${CLANG_TIDY_EXE}" "-checks=*,-fuchsia-*,-google-runtime-references")
set(CMAKE_CXX_CLANG_TIDY ${DO_CLANG_TIDY})
endif()
message(STATUS "The value of CMAKE_MODULE_PATH is: ${CMAKE_MODULE_PATH}")
find_package(OpenSSL REQUIRED)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
set(asio_header_dir "/home/book/test/asio/asio/install/include" CACHE PATH "Path to the asio library used.")
add_library(asio INTERFACE)
target_include_directories(asio INTERFACE asio_header_dir)
add_library(asio_dtls INTERFACE)
target_include_directories(asio_dtls INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_definitions(asio_dtls INTERFACE ASIO_HEADER_ONLY)
target_link_libraries(asio_dtls INTERFACE OpenSSL::SSL OpenSSL::Crypto Threads::Threads)
set(asio_dtls_sources
include/asio/ssl/dtls/impl/context.ipp
include/asio/ssl/dtls/detail/impl/engine.ipp)
set(ASIO_DTLS_PUBLIC_HEADERS
asio/dtls.hpp
asio/ssl/dtls/acceptor.hpp
asio/ssl/dtls/context.hpp
asio/ssl/dtls/default_cookie_generator.hpp
asio/ssl/dtls/socket.hpp
)
option(asio_build_dtls_static "Build asio_dtls as static library" OFF)
if(asio_build_dtls_static)
add_library(asio_dtls_static STATIC ${asio_dtls_sources})
target_include_directories(asio_dtls_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(asio_dtls_static OpenSSL::SSL OpenSSL::Crypto Threads::Threads)
endif(asio_build_dtls_static)
message(STATUS "CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}")
set(THREADS_PTHREAD_LIB "pthread")
message(STATUS "CMAKE_THREAD_LIBS_INIT=${CMAKE_THREAD_LIBS_INIT}")
option(asio_build_dtls_shared "Build asio_dtls as shared library" ON)
if(asio_build_dtls_shared)
add_library(asio_dtls_shared SHARED ${asio_dtls_sources})
set_target_properties(asio_dtls_shared PROPERTIES LINKER_LANGUAGE CXX)
target_include_directories(asio_dtls_shared PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(asio_dtls_shared OpenSSL::SSL OpenSSL::Crypto Threads::Threads)
endif(asio_build_dtls_shared)
#add_subdirectory(src)
这里测试程序没有编译过,大概是因为测试程序用到了boost,但是我并没有编译boost.
下面展示的是Toolchain.cmake
# Toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER arm-buildroot-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER arm-buildroot-linux-gnueabihf-g++)
编译命令:
cmake -DCMAKE_TOOLCHAIN_FILE=../Toolchain.cmake ..
重要知识点:
find_package 搜索FindXXX.cmake 的默认目录是/usr/share/cmake-xxx/Modules/
set_target_properties(asio_dtls_shared PROPERTIES LINKER_LANGUAGE CXX)
豆包说:“属性设置项。LINKER_LANGUAGE
是一个属性,用于指定链接器在链接目标时使用的编程语言。CXX
表示 C++ 语言,即告诉 CMake 在链接 asio_dtls_shared
目标时使用 C++ 链接器。”
当时没写这个报了“CMake Error: CMake can not determine linker language for target: asio_dtls_shared
CMake Error: Cannot determine link language for target "asio_dtls_shared".
”错。