CMake linker not working correctly - c++

I am trying to build my project using CMake but I am having error linking required libraries. I have this CMakeLists.txt in the root folder of my project:
cmake_minimum_required(VERSION 2.6)
project(test)
add_subdirectory(src)
And in my src folder, alongside my source files I have this CMakeLists.txt:
set (CMAKE_CXX_FLAGS "Wall -std=c++11" )
set (CMAKE_EXE_LINKER_FLAGS "-lSDL2 -lGL" )
file (GLOB SRCS *.cpp *.h )
add_executable(engine ${SRCS} )
I then go into the build folder and do cmake .. and it runs without any errors. When I do make, the compilation runs without any errors as well, but when it gets to the linking part, I get these errors:
CMakeFiles/test.dir/Application.cpp.o: In function `Application::onExecute()':
Application.cpp:(.text+0x41): undefined reference to `SDL_GetTicks'
Application.cpp:(.text+0x4e): undefined reference to `SDL_GetTicks'
Application.cpp:(.text+0xd7): undefined reference to `SDL_PollEvent'
CMakeFiles/test.dir/Application.cpp.o: In function `Application::render()':
Application.cpp:(.text+0x17b): undefined reference to `glClearColor'
Application.cpp:(.text+0x185): undefined reference to `glClear'
Application.cpp:(.text+0x194): undefined reference to `SDL_GL_SwapWindow'
CMakeFiles/test.dir/Application.cpp.o: In function `Application::cleanUp()':
Application.cpp:(.text+0x1b2): undefined reference to `SDL_GL_DeleteContext'
Application.cpp:(.text+0x1c1): undefined reference to `SDL_DestroyWindow'
Application.cpp:(.text+0x1c6): undefined reference to `SDL_Quit'
CMakeFiles/test.dir/Application.cpp.o: In function `Application::initialize()':
Application.cpp:(.text+0x1de): undefined reference to `SDL_Init'
Application.cpp:(.text+0x1ea): undefined reference to `SDL_GetError'
Application.cpp:(.text+0x22b): undefined reference to `SDL_CreateWindow'
Application.cpp:(.text+0x243): undefined reference to `SDL_GetError'
Application.cpp:(.text+0x25a): undefined reference to `SDL_Quit'
Application.cpp:(.text+0x270): undefined reference to `SDL_GL_SetAttribute'
Application.cpp:(.text+0x27f): undefined reference to `SDL_GL_SetAttribute'
Application.cpp:(.text+0x28e): undefined reference to `SDL_GL_SetAttribute'
Application.cpp:(.text+0x29d): undefined reference to `SDL_GL_CreateContext'
Application.cpp:(.text+0x2af): undefined reference to `glGetString'
collect2: error: ld returned 1 exit status
make[2]: *** [src/test] Error 1
make[1]: *** [src/CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2
I have the correct includes in my header files and I was able to compile and run using only make so I think the linker flags I tell CMake aren't being passed to the compiler. How can I fix this?

You have to use the cmake command target_link_libraries to reference SDL libs.

Related

linked library is valid in CMakeLists, but doesn't link at compile time

I'm just starting around with messing around with vulkan, and GLFW, but when I try to compile a test program, it gives me a bunch of linker errors:
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::Init()':
loops.cpp:(.text+0xd): undefined reference to `glfwInit'
/usr/bin/ld: loops.cpp:(.text+0x1c): undefined reference to `glfwWindowHint'
/usr/bin/ld: loops.cpp:(.text+0x2b): undefined reference to `glfwWindowHint'
/usr/bin/ld: loops.cpp:(.text+0x4f): undefined reference to `glfwCreateWindow'
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::Update()':
loops.cpp:(.text+0xa3): undefined reference to `glfwPollEvents'
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::DeInit()':
loops.cpp:(.text+0xcd): undefined reference to `glfwDestroyWindow'
/usr/bin/ld: loops.cpp:(.text+0xd2): undefined reference to `glfwTerminate'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/vulkan_test.dir/build.make:113: vulkan_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/vulkan_test.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project(vulkan_test)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH /home/headass/CMake_Modules/)
find_package(GLFW REQUIRED)
find_package(Vulkan REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS} ${VULKAN_INCLUDE_DIRS})
add_executable(vulkan_test main.cpp loops.cpp)
target_link_libraries(vulkan_test ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
Any idea why this is happening? I have both vulkan and GLFW installed, libglfw.so is in my /usr/lib/ directory, and clangd doesn't see anything wrong with it, but it still fails to link properly.
And yes, I HAVE tried googling this, to no avail.
Changing:
target_link_libraries(vulkan_test ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
to:
target_link_libraries(vulkan_test glfw ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
worked flawlessly

Problems setting external headers and linker libraries in CLion and Cmake [duplicate]

This question already has answers here:
CMake link to external library
(6 answers)
Closed 2 years ago.
I've just started to use CLion as my IDE after mainly using code::blocks on Linux for the last year or so. However I'm finding it really difficult to set up a project that worked and compiled fine in code::blocks because I just don't understand cmake yet.
Here's a quick description of my project - it consists of a main file that is calling a few header and source files, which all in the same directory. In the main file I'm also calling an external header file ("coupling.h", which is located in "/opt/package/API_SRC_Files/Coupling") which describes some classes relating to coupling to that software. The "coupling.h" file in turn references some files in "/opt/package/API_SRC_Files/Coupling". I also need to link to a shared library file "libcoupling.so" that is located in "/opt/package/lib".
Setting this up in code::blocks was pretty easy - just go to build options, and the respective paths to the search directories, and the path to the linked file and then build. Compiles in a few seconds.
I've tried to setup the project in CLion and CMake but I'm truly lost and I don't really understand why CMake is not finding the "coupling.h" file and throws countless "undefined reference to " errors. I'm sure I'll also have a problem setting up the shared library and I'm scared to even think about the difference in debug and release versions at the moment!
Here's my current CMake.txt file, hopefully someone can help. I'm using CLion 2020.1 on Fedora.
cmake_minimum_required(VERSION 3.16)
project(MBD VERSION 0.6.1)
set(CMAKE_CXX_STANDARD 14)
# add extra include directories
include_directories(.)
include_directories(/opt/package/API_SRC_Files)
include_directories(/opt/package/API_SRC_Files/Core)
include_directories(/opt/package/API_SRC_Files/Coupling)
set(PROJECT_HEADERS
coupling_utilities.h
geometry.h
io.h
shapelib.h
pid.h
spline.h
)
set(PROJECT_SOURCES
main.cpp
io.cpp
coupling_utilities.cpp
shapelib.cpp
pid.cpp
)
# add extra lib directories
link_directories(/opt/package/lib)
add_executable(MBD ${PROJECT_SOURCES} ${PROJECT_HEADERS})
Here's the error log from the output related to the include_directories():
/snap/clion/114/bin/cmake/linux/bin/cmake --build /home/user/CLionProjects/mbd/cmake-build-debug --target mbd -- -j 24
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/CLionProjects/mbd/cmake-build-debug
[ 16%] Linking CXX executable mbd
/usr/bin/ld: CMakeFiles/mbd.dir/main.cpp.o: in function `main':
/home/user/CLionProjects/mbd/main.cpp:224: undefined reference to `NApi::Coupling::getGeometryId(char const*, int&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:260: undefined reference to `NApi::Coupling::getTimeStep(double&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:268: undefined reference to `NApi::Coupling::getTime(double&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:273: undefined reference to `NApi::Coupling::setTime(double const&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:276: undefined reference to `NApi::Coupling::getTime(double&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:285: undefined reference to `NApi::Coupling::setGridCellSize(double const&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:288: undefined reference to `NApi::Coupling::setNumberOfCores(int const&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:343: undefined reference to `NApi::Coupling::getGeometryPosition(int, NApi::C3dValue&, NApi::C3x3Matrix&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:345: undefined reference to `NApi::Coupling::getGeometryTranslation(int, NApi::C3dValue&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:348: undefined reference to `NApi::Coupling::getGeometryVelocity(int, NApi::C3dValue&, NApi::C3dValue&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:442: undefined reference to `NApi::Coupling::isConnected() const'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:444: undefined reference to `NApi::Coupling::getGeometryForces(int, NApi::C3dValue&, NApi::C3dValue&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:531: undefined reference to `NApi::Coupling::isConnected() const'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:534: undefined reference to `NApi::Coupling::setGeometryMotion(int, NApi::C3dValue const&, NApi::C3x3Matrix const&, NApi::C3dValue const&, NApi::C3dValue const&, double, bool)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:554: undefined reference to `NApi::Coupling::isConnected() const'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:556: undefined reference to `NApi::Coupling::simulate(double const&, double)'
/usr/bin/ld: CMakeFiles/mbd.dir/main.cpp.o: in function `__static_initialization_and_destruction_0(int, int)':
/home/user/CLionProjects/mbd/main.cpp:74: undefined reference to `NApi::Coupling::ICoupling()'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:74: undefined reference to `NApi::Coupling::~ICoupling()'
/usr/bin/ld: CMakeFiles/mbd.dir/coupling_utilities.cpp.o: in function `connect(NApi::Coupling&)':
/home/user/CLionProjects/mbd/coupling_utilities.cpp:54: undefined reference to `NApi::Coupling::initialiseCoupling()'
/usr/bin/ld: /home/user/CLionProjects/mbd/coupling_utilities.cpp:67: undefined reference to `NApi::Coupling::connectCoupling(bool, char const*)'
/usr/bin/ld: /home/user/CLionProjects/mbd/coupling_utilities.cpp:90: undefined reference to `NApi::Coupling::connectCoupling(bool, char const*)'
/usr/bin/ld: CMakeFiles/mbd.dir/coupling_utilities.cpp.o: in function `getResumeTimestep(NApi::Coupling&, double, double)':
/home/user/CLionProjects/mbd/coupling_utilities.cpp:109: undefined reference to `NApi::Coupling::getNumberOfTimesteps(unsigned int&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/coupling_utilities.cpp:136: undefined reference to `NApi::Coupling::getTimesteps(double*, unsigned int&, unsigned int)'
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/mbd.dir/build.make:144: mbd] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/mbd.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/mbd.dir/rule] Error 2
gmake: *** [Makefile:118: mbd] Error 2
Here is how I would write the cmakelists.txt file:
cmake_minimum_required(VERSION 3.16)
project(MBD VERSION 0.6.1)
set(CMAKE_CXX_STANDARD 14)
set(API_SRC_Files /opt/package/API_SRC_Files) # ${API_SRC_Files}
set(SRC_FILES
main.cpp
coupling_utilities.h coupling_utilities.cpp
geometry.h
io.h io.cpp
pid.h pid.cpp
shapelib.h shapelib.cpp
spline.h
)
# add extra include directories
include_directories(
includes
.
${API_SRC_Files}
${API_SRC_Files}/Core
${API_SRC_Files}/Coupling
)
# add extra lib directories
link_directories(/opt/package/lib)
add_executable(${PROJECT_NAME} ${SRC_FILES})
Use something like the API_SRC_Files variable for two reasons: reduces errors from having to repeat the same path information, makes it easier if the library is ever moved to a new location. The same thing with using the PROJECT_NAME variable. The comment is to remind myself how to utilize the variable later on in the file.
Combine your SRC_FILES and SRC_HEADERS into one array with source and matching header on the same line. This helps to make sure both are listed. Please note when you tell Clion you want to add a new C++ Class, it will be in the form class.cpp class.h, and not alphabetized. I always list the main.cpp, first.
The dot, ., in the include_directories may or may not be necessary.
Hope this helps.

cmake + g++ fail linking winsock2.h library [duplicate]

This question already has answers here:
How to add "-l" (ell) compiler flag in CMake
(2 answers)
How to set library flags after source file in cmake?
(1 answer)
Closed 2 years ago.
I have cmake 3.17.1 , g++ 9.2.0. Trying to build my project :
cmake_minimum_required(VERSION 3.17.1)
project("Client")
SET( CMAKE_MAKE_PROGRAM C:/Strawberry/c/bin/mingw32-make.exe FORCE )
set(CMAKE_CXX_FLAGS "-std=c++17 -lwsock32 -lws2_32" )
add_executable(Client
main.cpp
client.cpp
client.h
logmsg.cpp
includes.h
)
it compiles but linking fails with errors:
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0x144): undefined reference to `__imp_WSAStartup'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0x270): undefined reference to `__imp_inet_addr'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0x28b): undefined reference to `__imp_htons'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0x504): undefined reference to `__imp_socket'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0x540): undefined reference to `__imp_connect'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0x57d): undefined reference to `__imp_WSAGetLastError'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0x7ac): undefined reference to `__imp_recv'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0x895): undefined reference to `__imp_WSAGetLastError'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0xbdd): undefined reference to `__imp_send'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0xc02): undefined reference to `__imp_WSAGetLastError'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0xcc6): undefined reference to `__imp_send'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0xce4): undefined reference to `__imp_WSAGetLastError'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0xe2e): undefined reference to `__imp_send'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0xe4c): undefined reference to `__imp_WSAGetLastError'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0xfb2): undefined reference to `__imp_send'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0xfd0): undefined reference to `__imp_WSAGetLastError'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0x113c): undefined reference to `__imp_shutdown'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0x1153): undefined reference to `__imp_closesocket'
CMakeFiles\Client.dir/objects.a(client.cpp.obj):client.cpp:(.text+0x115c): undefined reference to `__imp_WSACleanup'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeFiles\Client.dir\build.make:135: Client.exe] Error 1
make[1]: *** [CMakeFiles\Makefile2:95: CMakeFiles/Client.dir/all] Error 2
make: *** [makefile:103: all] Error 2
Placed screenshoot also here: https://imgur.com/a/SFvYSfm .
I cannot understand why
-lwsock32 -lws2_32
flags do not solve it. What am i doing wrong?

MinGW/CMake Undefined Reference to ZLib

I have a project I want to build with CMake and compile with MinGW. The project uses Zlib. When I build with CMake I get no errors but then when I run MinGW Make it gives the following output:
C:\Projects\MultiMCBuild>C:\Qt\Tools\mingw492_32\bin\mingw32-make.exe
.
.
.
[ 50%] Linking CXX shared library ..\libMultiMC_logic.dll
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x1f6c): undefined reference to 'z_inflateEnd'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x29e2): undefined reference to 'z_inflateInit2_'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x2a6d): undefined reference to 'z_get_crc_table'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x2ca7): undefined reference to 'z_inflateEnd'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x2f52): undefined reference to 'z_inflateInit2_'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x2f77): undefined reference to 'z_inflateEnd'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x3239): undefined reference to 'z_inflateInit2_'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x3317): undefined reference to 'z_inflateEnd'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x3626): undefined reference to 'z_crc32'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x371f): undefined reference to 'z_inflate'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x376a): undefined reference to 'z_crc32'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x3a57): undefined reference to 'z_inflateEnd'
C:/Qt/Tools/mingw492_32/bin/../lib/gcc/i686-w64- mingw32/4.9.2/../../../../i686-w64-mingw32/bin/ld.exe: C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj): bad reloc address 0x20 in section `.eh_frame'
collect2.exe: error: ld returned 1 exit status
logic\CMakeFiles\MultiMC_logic.dir\build.make:3186: recipe for target 'libMultiMC_logic.dll' failed
mingw32-make[2]: * * * [libMultiMC_logic.dll] Error 1
CMakeFiles\Makefile2:1806: recipe for target 'logic/CMakeFiles/MultiMC_logic.dir/all' failed
mingw32-make[1]: * * * [logic/CMakeFiles/MultiMC_logic.dir/all] Error 2
makefile:159: recipe for target 'all' failed
mingw32-make: * * * [all] Error 2
Anyone have a clue what I can do to fix this? I read that the code can't find the ZLib library, how do I link it?
EDIT here is my CMakeLists.txt. I got this from the Git project.
I just tried to generate a small example to reproduce your errors. I took my test file from the home page of zlib http://zlib.net/zpipe.c
My initial CMakeLists.txt was
cmake_minimum_required(VERSION 3.4)
project(zlib_test)
set(ZLIB_TEST_SOURCES zpipe.c)
add_executable(${PROJECT_NAME} ${ZLIB_TEST_SOURCES})
And I got the same errors
[ 50%] Building C object CMakeFiles/zlib_test.dir/zpipe.c.o
[100%] Linking C executable zlib_test
CMakeFiles/zlib_test.dir/zpipe.c.o: In function `def':
zpipe.c:(.text+0x65): undefined reference to `deflateInit_'
zpipe.c:(.text+0xcd): undefined reference to `deflateEnd'
zpipe.c:(.text+0x135): undefined reference to `deflate'
zpipe.c:(.text+0x1cf): undefined reference to `deflateEnd'
zpipe.c:(.text+0x25d): undefined reference to `deflateEnd'
CMakeFiles/zlib_test.dir/zpipe.c.o: In function `inf':
zpipe.c:(.text+0x2eb): undefined reference to `inflateInit_'
zpipe.c:(.text+0x353): undefined reference to `inflateEnd'
zpipe.c:(.text+0x3a4): undefined reference to `inflate'
zpipe.c:(.text+0x404): undefined reference to `inflateEnd'
zpipe.c:(.text+0x476): undefined reference to `inflateEnd'
zpipe.c:(.text+0x4a6): undefined reference to `inflateEnd'
collect2: error: ld returned 1 exit status
make[2]: *** [zlib_test] Error 1
make[1]: *** [CMakeFiles/zlib_test.dir/all] Error 2
make: *** [all] Error 2
After changing CMakeLists.txt to this form
cmake_minimum_required(VERSION 3.4)
project(zlib_test)
find_package(ZLIB REQUIRED)
if (ZLIB_FOUND)
include_directories(${ZLIB_INCLUDE_DIRS})
endif()
set(ZLIB_TEST_SOURCES zpipe.c)
add_executable(${PROJECT_NAME} ${ZLIB_TEST_SOURCES})
target_link_libraries(${PROJECT_NAME} ${ZLIB_LIBRARIES})
I could compile the program.
So your problem is: Where is ZLIB added in your CMakeLists.txt? At least, you need the line find_package(ZLIB REQUIRED).

Glew undefined reference linking error

I'm running into a linking error from cmake when I attempt to build my project in CLion. I've tried what the other threads said: Putting opengl last and glu first, changing the order of my includes and setting the cmake option GLEW_STATIC but none of them have fixed it or even given different errors.
I have used glew specifically compiled for mingw32 (From https://julianibarz.wordpress.com/2010/05/12/glew-1-5-4-mingw32/) and I have compiled GLEW myself but I still have the same problems..
Here is my CMake file:
cmake_minimum_required(VERSION 3.2)
project(3D_prototyping)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package (OpenGL REQUIRED)
if (WIN32)
else (WIN32)
find_package (glfw3 REQUIRED)
endif (WIN32)
set(SOURCE_FILES main.cpp)
add_executable(3D_prototyping ${SOURCE_FILES})
target_link_libraries (3D_prototyping
${GLFW3_LIBRARY}
${OPENGL_LIBRARIES}
${GLEW_LIBRARY}
${COCOA_LIBRARY} ${COREVID_LIBRARY} ${IOKIT_LIBRARY})
if (WIN32)
target_link_libraries (3D_prototyping
${OPENGL_LIBRARIES} glfw3 glu32 opengl32)
endif (WIN32)
This is the error I'm getting:
"C:\Program Files (x86)\JetBrains\CLion 1.0.3\bin\cmake\bin\cmake.exe" --build C:\Users\Max\.clion10\system\cmake\generated\1d24224\1d24224\Debug --target 3D_prototyping -- -j 4
Linking CXX executable 3D_prototyping.exe
CMakeFiles\3D_prototyping.dir/objects.a(main.cpp.obj): In function `Z9getShaderPKcj':
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:27: undefined reference to `_imp____glewCreateShader'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:30: undefined reference to `_imp____glewShaderSource'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:31: undefined reference to `_imp____glewCompileShader'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:35: undefined reference to `_imp____glewGetShaderiv'
CMakeFiles\3D_prototyping.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:75: undefined reference to `_imp____glewCreateProgram'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:76: undefined reference to `_imp____glewAttachShader'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:77: undefined reference to `_imp____glewAttachShader'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:78: undefined reference to `_imp____glewBindFragDataLocation'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:79: undefined reference to `_imp____glewLinkProgram'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:80: undefined reference to `_imp____glewUseProgram'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:83: undefined reference to `_imp____glewGetAttribLocation'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:84: undefined reference to `_imp____glewVertexAttribPointer'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:85: undefined reference to `_imp____glewEnableVertexAttribArray'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:89: undefined reference to `_imp____glewGenVertexArrays'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:90: undefined reference to `_imp____glewBindVertexArray'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:101: undefined reference to `_imp____glewGenBuffers'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:102: undefined reference to `_imp____glewBindBuffer'
C:/Users/Max/ClionProjects/3D prototyping/main.cpp:103: undefined reference to `_imp____glewBufferData'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [3D_prototyping.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/3D_prototyping.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/3D_prototyping.dir/rule] Error 2
mingw32-make.exe: *** [3D_prototyping] Error 2
CMakeFiles\3D_prototyping.dir\build.make:86: recipe for target '3D_prototyping.exe' failed
CMakeFiles\Makefile2:59: recipe for target 'CMakeFiles/3D_prototyping.dir/all' failed
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/3D_prototyping.dir/rule' failed
Makefile:108: recipe for target '3D_prototyping' failed
Fixed it.
Somewhere along the lines I had got glu32 which glew was using instead of the windows ones I compiled. Added a reference to glew32 and it worked fine.