0

I'm trying to create Makefile for my project with CMake but I'm getting

CMake Error: Cannot determine link language for target "mylib".
CMake Error: CMake can not determine linker language for target: mylib

error.

Project structure is as follows:

.
├── 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

Where headers in headers_library1 includes system shared libraries (namely libcurl and libcryptopp) and header-only library headers_library2.

Root CMakeLists.txt contains:

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)

and lib/CMakeLists.txt contains:

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)

First I want to build static library mylib and place it into /lib directory. Then I want to build example executable and link it with the mylib library.

Petr Javorik
  • 1,695
  • 19
  • 25
  • You are only adding headers to your library. There's nothing to compile. Therefore you should create an INTERFACE library instead of a STATIC library. – vre Jul 23 '18 at 20:18
  • You case is described by [that answer](https://stackoverflow.com/a/29518149/3440745) - CMake cannot determine language for a library without any source file. (**Header files are not sources**.). You need to add at least one source files, or explicitly set a language via target properties. – Tsyvarev Jul 23 '18 at 20:18

0 Answers0