我正在尝试使用CMake为我的项目创建Makefile,但是我得到了
CMake Error: Cannot determine link language for target "mylib".
CMake Error: CMake can not determine linker language for target: mylib
错误。
项目结构如下:
.
├── CMakeLists.txt
├── build
├── doc
├── include
│ ├── headers_library1
│ │ ├── headers1.hpp
│ │ ├── headers2.hpp
│ │ └── headers3.hpp
│ └── headers_library2
│ ├── dir1
│ ├── dir2
│ ├── dir3
│ ├── headers1.h
│ ├── headers2.h
.
. many header files
.
│ ├── headers20.h
│ └── headers21.h
├── lib
│ └── CMakeLists.txt
└── src
└── example.cpp
headers_library1
中的标头包括系统共享库(即libcurl和libcryptopp)和仅标头的库headers_library2
。
根CMakeLists.txt
包含:
cmake_minimum_required (VERSION 3.12)
project (example)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_subdirectory(lib)
# TARGET example
add_executable (example src/example.cpp)
target_link_libraries (example PRIVATE mylib)
target_include_directories (example PUBLIC include)
target_compile_options(example PRIVATE -Wall)
和lib/CMakeLists.txt
包含:
file(GLOB_RECURSE HDRS_H ${PROJECT_SOURCE_DIR}/include/*.h)
file(GLOB_RECURSE HDRS_HPP ${PROJECT_SOURCE_DIR}/include/*.hpp)
# TARGET mylib
add_library (mylib STATIC ${HDRS_H} ${HDRS_HPP})
target_include_directories (mylib PUBLIC ${PROJECT_SOURCE_DIR}/include)
set_target_properties(mylib PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
首先,我想构建 static 库mylib
并将其放入/lib
目录中。然后,我想构建example
可执行文件并将其与mylib
库链接。