Callum
Callum

Reputation: 85

The target name "C:/path/to/lib/file.lib" is reserved or not valid for certain CMake features

I'm trying to write a simple C++ program and include the library for GLFW.

I'm using the CLion IDE and the following is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)
project(Engine)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE TRUE)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib/;)
add_library(${CMAKE_CURRENT_SOURCE_DIR}/lib/GLFW/libraries/glfw3.lib;)
add_executable(Engine main.cpp)

The error CMake throws is:

CMake Error at CMakeLists.txt:8 (add_library):
The target name
"C:/Users/callu/CLionProjects/Engine/lib/GLFW/libraries/glfw3.lib"
is reserved or not valid for certain CMake features, such as generator
expressions, and may result in undefined behavior.

My code is:

#include <iostream>
#include "GLFW/glfw3.h"

int main() {
    if (!glfwInit)
        std::cout << "Failure" << std::endl;
    else
        std::cout << "Success!" << std::endl;
    return 0;
}

I've looked around and found one possible reason is because the value of the library is used elsewhere by CMake. For example, if I used "test" as a library CMake would complain because test is a predefinied thing. I fail to see how the path to the glfw3 library is predefined by CMake. (If that's the issue.)

If it matters, my directory structure is:

C:/Users/callu/CLionProjects/Engine/
  ../lib
    ../GLFW
      glfw3.h
      glfw3Natives.h
      ../libraries
        glfw3.lib
        glfw3dll.lib
        glfw3.dll
  CMakeLists.txt
  main.cpp

Upvotes: 1

Views: 2813

Answers (3)

Yuki
Yuki

Reputation: 4173

If this is a target that you have already built outside the project, you have to import it with add_library(<name> IMPORTED). Besides, add_library's first argument is always <name>. That means you cannot put a path there.

Upvotes: 1

gordan.sikic
gordan.sikic

Reputation: 1640

Your problem is wrong usage of add_library. This statement is used to create a library (.dll/.lib on Windows or .so/.a on Unix) out of some set of sources, and you are trying to use it to list already compiled/linked libraries to be used for linking of your executable.

If my assumptions are correct, your CMakeLists.txt file should look as follows:

cmake_minimum_required(VERSION 3.12)
project(Engine)

set(CMAKE_CXX_STANDARD 17)
set (CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_VERBOSE_MAKEFILE TRUE)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)
add_executable(Engine main.cpp)
target_link_libraries (Engine ${CMAKE_CURRENT_SOURCE_DIR}/lib/GLFW/libraries/glfw3.lib)

Note target_link_libraries statement after add_executable. This is what you need to tell CMake which libraries you will use for linking.

BTW,
in case you want to enforce the C++17 standard, setting just the CMAKE_CXX_STANDARD variable is not enough, since it allows for (quiet) decay. In that case you also need to set variable CMAKE_CXX_STANDARD_REQUIRED.

At the end, I would strongly suggest to read CMake documentation:

And for your case:

Upvotes: 2

Callum
Callum

Reputation: 85

I found a fix! I moved the ../../libraries directory into a common /lib directory. I also replaced lines 6 & 7 in the CMakeLists.txt with

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include;) 
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)

Upvotes: 0

Related Questions