CMake is dropping the item c++
Asked Answered
S

3

11

I work on windows with QtCreator .I try to use freeglut/opengl2 all libs have been build and install properly But have undefined references to all glut components certainly due to : Targets may link only to libraries. CMake is dropping the item.

here my CMakelists.txt

project(Projet_AIN CXX)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ")
find_package(GLUT   REQUIRED)
find_package(GLEW   REQUIRED)
find_package(OpenGL REQUIRED)

include_directories(${GLEW_INCLUDE_DIR}
                    ${GLUT_INCLUDE_DIR}
                    ${OpenGL_INCLUDE_DIR})

link_directories(${GLEW_LIBRARIES_DIR}
                 ${GLUT_LIBRARY_DIR}
                 ${OpenGL_LIBRARY_DIR})

add_definitions(${OpenGL_DEFINITIONS}
                ${GLEW_DEFINITIONS}
                ${GLUT_DEFINITIONS})



set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE})
file(
    GLOB
    SOURCE_FILES
    include/*.hpp
    src/*.cpp
)


add_executable(
        ${PROJECT_NAME}
        ${SOURCE_FILES}
)
target_link_libraries(${PROJECT_NAME}
                  ${OPENGL_LIBRARY}
                  ${GLEW_LIBRARY}
                  ${GLUT_LIBRARY}
                  )

CMake output:

Running "C:\cmake\bin\cmake.exe C:/Users/Thibaut/Documents/Projet_Raffin "-GCodeBlocks - MinGW Makefiles" "-DCMAKE_BUILD_TYPE:STRING=Debug" "-DCMAKE_CXX_COMPILER:STRING=C:/MinGW/bin/g++.exe" "-DCMAKE_C_COMPILER:STRING=C:/MinGW/bin/gcc.exe" "-DCMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}" "-DGLEW_INCLUDE_DIR:PATH=C:/Program Files (x86)/glew/include" "-DGLEW_LIBRARY:FILEPATH=C:/Program Files (x86)/glew/lib" "-DGLUT_INCLUDE_DIR:PATH=C:/Program Files (x86)/freeglut/include" "-DGLUT_glut_LIBRARY:FILEPATH=C:/Program Files (x86)/freeglut/lib" "-DQT_QMAKE_EXECUTABLE:STRING="" in C:\Users\Thibaut\Documents\build-Projet_Raffin-Kat-Debug.
-- Configuring done
WARNING: Target "Projet_AIN" requests linking to directory "C:/Program Files (x86)/glew/lib".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "Projet_AIN" requests linking to directory "C:/Program Files (x86)/freeglut/lib".  Targets may link only to libraries.  CMake is dropping the item.
-- Generating done
-- Build files have been written to: C:/Users/Thibaut/Documents/build-Projet_Raffin-Kat-Debug

Make output:

14:26:39: Exécution des étapes pour le projet Projet_AIN...
14:26:39: Running CMake in preparation to build...
14:26:39: Débute : "C:\cmake\bin\cmake.exe" --build . --target all
[ 50%] Building CXX object CMakeFiles/Projet_AIN.dir/src/main.cpp.obj
[100%] Linking CXX executable bin\Debug\Projet_AIN.exe
CMakeFiles\Projet_AIN.dir/objects.a(main.cpp.obj): In function `glutInit_ATEXIT_HACK':
C:/PROGRA~2/freeglut/include/GL/freeglut_std.h:637: undefined reference to `__imp___glutInitWithExit'
CMakeFiles\Projet_AIN.dir/objects.a(main.cpp.obj): In function `glutCreateWindow_ATEXIT_HACK':
C:/PROGRA~2/freeglut/include/GL/freeglut_std.h:639: undefined reference to `__imp___glutCreateWindowWithExit'
CMakeFiles\Projet_AIN.dir/objects.a(main.cpp.obj): In function `glutCreateMenu_ATEXIT_HACK':
C:/PROGRA~2/freeglut/include/GL/freeglut_std.h:641: undefined reference to `__imp___glutCreateMenuWithExit'
CMakeFiles\Projet_AIN.dir/objects.a(main.cpp.obj): In function `action_timer(int)':
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:30: undefined reference to `__imp_glutPostRedisplay'
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:31: undefined reference to `__imp_glutTimerFunc'
CMakeFiles\Projet_AIN.dir/objects.a(main.cpp.obj): In function `RenderScene()':
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:71: undefined reference to `__imp_glutWireTeapot'
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:73: undefined reference to `__imp_glutSwapBuffers'
CMakeFiles\Projet_AIN.dir/objects.a(main.cpp.obj): In function `callback_Keyboard(unsigned char, int, int)':
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:80: undefined reference to `__imp_glutLeaveMainLoop'
CMakeFiles\Projet_AIN.dir/objects.a(main.cpp.obj): In function `InitializeGL()':
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:113: undefined reference to `__imp_glutTimerFunc'
CMakeFiles\Projet_AIN.dir/objects.a(main.cpp.obj): In function `GlewInit()':
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:118: undefined reference to `__imp_glewInit'
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:122: undefined reference to `__imp_glewGetErrorString'
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:125: undefined reference to `__imp_glewGetString'
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:127: undefined reference to `__imp_glewIsSupported'
CMakeFiles\Projet_AIN.dir/objects.a(main.cpp.obj): In function `InitializeGlutCallbacks':
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:141: undefined reference to `__imp_glutDisplayFunc'
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:148: undefined reference to `__imp_glutKeyboardFunc'
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:154: undefined reference to `__imp_glutReshapeFunc'
CMakeFiles\Projet_AIN.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:167: undefined reference to `__imp_glutInitDisplayMode'
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:169: undefined reference to `__imp_glutInitWindowPosition'
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:170: undefined reference to `__imp_glutInitWindowSize'
C:/Users/Thibaut/Documents/Projet_Raffin/src/main.cpp:182: undefined reference to `__imp_glutMainLoop'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [CMakeFiles\Projet_AIN.dir\build.make:97: bin/Debug/Projet_AIN.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:67: CMakeFiles/Projet_AIN.dir/all] Error 2
mingw32-make.exe: *** [Makefile:83: all] Error 2
14:26:42: Le processus "C:\cmake\bin\cmake.exe" s'est terminé avec le code 2.
Erreur lors de la compilation/déploiement du projet Projet_AIN (kit : Kat)
When executing step "Make"
Sachsse answered 18/3, 2017 at 14:2 Comment(0)
A
13

Fundamentally, to get rid of Targets may link only to libraries.CMake is dropping the item. error, you need to try linking only the path to lib and not the lib directory

For instance:

This error happens if you specify:

FREETYPE_LIBRARY_REL = H:\OgreSDK_vc9_v1-7-1\lib\release

You need to specify path to the .lib:

FREETYPE_LIBRARY_REL = H:\OgreSDK_vc9_v1-7-1\lib\release\freetype.lib 

Thanks to Link

Aeonian answered 20/5, 2019 at 9:17 Comment(0)
B
1

Modern way for script, called by find_package(XXX), to name library containing variable is XXX_LIBRARIES.

As for XXX_LIBRARY variable, it sometimes refers to the same (list of libraries), or to the directory with the libraries. Looks like the second case is yours, that is why you get that error.

If you want to know names of the variables which are set by find_package(XXX), see that question.

Biannulate answered 18/3, 2017 at 21:43 Comment(1)
Well cmake try to link with directories instead of lib, i solve the problem thanksSachsse
A
1

Your link command should be

target_link_libraries(${PROJECT_NAME}
                  ${OPENGL_LIBRARIES}
                  ${GLEW_LIBRARIES}
                  ${GLUT_LIBRARIES}
                  )

Also, the link_directories() and add_definitions() commands are unnecessary.
Since you are supplying the full paths to the libraries in target_link_libraries(), you do not need to supply the directories again.

To check which variables a find_package() command sets, check the documentation for that package in the CMake docs. For example, the OpenGL page is here.

Amanda answered 19/3, 2017 at 2:45 Comment(2)
I replace library to libraries and also remove add_definition and link_directories but i got same errorSachsse
Note, the linked libraries paths should actually specify not just the directory of the path, but the full path to the actual library (.so or .a file). E.g. set(SOME_PATH_TO_STATIC_LIB someRelativePath/someLib/x86/lib.a) set(SOME_PATH_TO_DYNAMIC_LIB someRelativePath/someLib/x86/lib.so)Successive

© 2022 - 2024 — McMap. All rights reserved.