#Build Imgui with CMake get `unresolved external symbol` error

19 messages · Page 1 of 1 (latest)

pallid isle
#

when I build the project by cmake and vscode, it said __imp_glClearmain, __imp_glClearColormain, __imp_glViewportmain are unresolved external symbol
stackoverflow said it because it cant find opengl32.lib, but I did link the lib"""

this is my structure:

MesOpengl
├── include
│   └── glad
│       ├── glad.c
│       └── glad.h 
│   └── GLFW
│       ├── glfw3.h
│       └── glfw3native.h
│   └── KHR
│       └── glfw3native.h
│   └── imgui
│       ├── imgui.h
│       ├── imconfig.h
│       ├── imgui_impl_glfw.h
│       ├── imgui_impl_opengl3_loader.h
│       ├── imgui_impl_opengl3.h
│       ├── imgui_internal.h
│       ├── imstb_rectpack.h
│       ├── imstb_textedit.h
│       ├── imstb_truetype.h
│       ├── imgui_demo.cpp
│       ├── imgui_draw.cpp
│       ├── imgui_tables.cpp
│       ├── imgui_widgets.cpp
│       ├── imgui.cpp
│       ├── imgui_impl_opengl3.cpp
│       └── imgui_impl_glfw.cpp
├── lib
│   ├── glfw3.lib
│   ├── opengl32.lib
│   └── opengl32.dll
├── src
│   └── main.cpp
└── CMakeLists.txt

and this is my CMakeLists:

cmake_minimum_required(VERSION 3.8)
 
project(MesOpengl VERSION 0.1.0)
 
set(CMAKE_CXX_STANDARD 11)
 
set(THIRD_PARTY_H ${PROJECT_SOURCE_DIR}/include)
include_directories(${THIRD_PARTY_H})

set(LIB_PATH ${PROJECT_SOURCE_DIR}/lib)
file(GLOB_RECURSE LIB_FILES
    "${LIB_PATH}/*.lib"
)
link_directories(${LIB_PATH})

set(IMGUI_DIR ${THIRD_PARTY_H}/imgui)
add_library(IMGUI STATIC)

target_sources( IMGUI
                PUBLIC
                ${IMGUI_DIR}/imgui.h
                ${IMGUI_DIR}/imconfig.h
                ${IMGUI_DIR}/imgui_impl_glfw.h
                ${IMGUI_DIR}/imgui_impl_opengl3_loader.h
                ${IMGUI_DIR}/imgui_impl_opengl3.h
                ${IMGUI_DIR}/imgui_internal.h
                ${IMGUI_DIR}/imstb_rectpack.h
                ${IMGUI_DIR}/imstb_textedit.h
                ${IMGUI_DIR}/imstb_truetype.h

                PUBLIC
                ${IMGUI_DIR}/imgui_demo.cpp
                ${IMGUI_DIR}/imgui_draw.cpp
                ${IMGUI_DIR}/imgui_tables.cpp
                ${IMGUI_DIR}/imgui_widgets.cpp
                ${IMGUI_DIR}/imgui.cpp
                ${IMGUI_DIR}/imgui_impl_opengl3.cpp
                ${IMGUI_DIR}/imgui_impl_glfw.cpp
                )

target_include_directories( IMGUI
                            PUBLIC ${IMGUI_DIR}
                            PUBLIC ${THIRD_PARTY_H}
                            )

                            
target_link_libraries(IMGUI PUBLIC ${LIB_FILES})
target_link_libraries(IMGUI PUBLIC ${CMAKE_DL_LIBS})
target_link_libraries(IMGUI PUBLIC ${OPENGL_gl_LIBRARY})

aux_source_directory(./src DIR_SRCS)
add_executable(${CMAKE_PROJECT_NAME} ${DIR_SRCS})
target_link_libraries(${CMAKE_PROJECT_NAME} ${LIB_FILES})
target_link_libraries(${CMAKE_PROJECT_NAME} IMGUI)
simple lavaBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

simple lavaBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

boreal prism
#

Show the compile commands used in the build?

pallid isle
#

I use this command in vscode command line

mkdir build
cd build
cmake --build . -j4
pallid isle
#

when I save the CMakeLists, vscode will do the configuration for me

#

this is my screenshot of file structure

boreal prism
#

Then look at the libraries listed as -lfoo in the link stage.

pallid isle
#

did u mean this?

#

I use cmake --build . -j4 -v

#

the error said "unresolved external symbol"

boreal prism
#

Are you building glad.c as part of the project?

#

If not, you'll have to.

pallid isle
#

I think yes, I set the include directories, and glad.c is included in the folder:

set(THIRD_PARTY_H ${PROJECT_SOURCE_DIR}/include)
include_directories(${THIRD_PARTY_H})
tawny aspen
pallid isle
#

I solved it, the problem is my opengl32.lib is copy from x86 folder but I build it as x64, Im sorry

#

!solved