Library not found (Windows)

Hello! This is my first post here!

I am trying to do my first project with CMake and I am stuck. I am working with windows.

Please, can you help me?

Currently, there are two issues:

  • I am trying to include an external library called GCLib, but CMake does not find it.
  • If I copy the library to the output folder then a message appears telling me that there are undefined symbols. I included gc.h but (I suppose) the linker is not linking them properly:

Severity Code Description Project File Line Suppression State
Error LNK1120 2 unresolved externals C:\multiDimIndex\out\build\x64-Debug\multiDimIndex C:\multiDimIndex\out\build\x64-Debug\multiDimIndex.exe 1
Error LNK2019 unresolved external symbol __imp_GC_init referenced in function main C:\multiDimIndex\out\build\x64-Debug\multiDimIndex C:\multiDimIndex\out\build\x64-Debug\multiDimIndex.cpp.obj 1
Error LNK2019 unresolved external symbol __imp_GC_malloc referenced in function main C:\multiDimIndex\out\build\x64-Debug\multiDimIndex C:\multiDimIndex\out\build\x64-Debug\multiDimIndex.cpp.obj 1

My current file list:

My current CMakeList is:

CMakeList.txt : CMake project for multiDimIndex, include source and define

project specific logic here.

project(multiDimIndex)
cmake_minimum_required (VERSION 3.8)
message(STATUS “--------------------- Testing”)

Add source to this project’s executable.

message("---- CMAKE_SOURCE_DIR = " ${CMAKE_SOURCE_DIR})

include_directories(${CMAKE_SOURCE_DIR}/gclib)
link_directories(${CMAKE_SOURCE_DIR}/gclib)
find_path(GCLIB_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/gclib/gc.h)
find_library(GCLIB_LIBRARY gc-lib)

message (“-- " ${CMAKE_SOURCE_DIR}/gclib/gc.h)
message(”-- " ${GCLIB_INCLUDE_DIR})
message("-- " ${GCLIB_LIBRARY})
set(YOUR_LIBRARIES)

if (GCLIB_INCLUDE_DIR AND GCLIB_LIBRARY)
# you may need that if further action in your CMakeLists.txt depends
# on detecting your library
set(GCLIB_FOUND TRUE)

# you may need that if you want to conditionally compile some parts
# of your code depending on library availability
add_definitions(-DHAVE_GCLIB=1)

# those two, you really need
include_directories(${GCLIB_INCLUDE_DIR})
set(YOUR_LIBRARIES ${YOUR_LIBRARIES} ${GCLIB_LIBRARY})

endif ()

configure_file(multiDimIndex.config.h.in multiDimIndex.config.h) #fichero donde poner configuraciones para el proyecto ()

add_library(format format.cpp)

add_executable (multiDimIndex “multiDimIndex.cpp” “multiDimIndex.h” ) #“…/PruebaGC/include/gc.h”) “./gc-lib/gc.h”
#target_link_libraries(multiDimIndex ${YOUR_LIBRARIES})
set(YOUR_LIBRARIES ${YOUR_LIBRARIES} format)
message ("=== libraries " ${YOUR_LIBRARIES})
target_link_libraries(multiDimIndex ${YOUR_LIBRARIES})
#target_link_libraries(multiDimIndex gc-lib format)

target_include_directories(multiDimIndex PUBLIC
“${PROJECT_BINARY_DIR}”
)

When CMake is run, this is the output:

2> CMake generation started for configuration: ‘x64-Debug’.
2> Command line: “cmd.exe” /c ““C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe” -G “Ninja” -DCMAKE_INSTALL_PREFIX:PATH=“C:\multiDimIndex\out\install\x64-Debug” -DCMAKE_CXX_COMPILER:FILEPATH=“C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/HostX64/x64/cl.exe” -DCMAKE_C_COMPILER:FILEPATH=“C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/HostX64/x64/cl.exe” -DCMAKE_BUILD_TYPE=“Debug” -DCMAKE_MAKE_PROGRAM=“C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe” “C:\multiDimIndex” 2>&1”
2> Working directory: C:\multiDimIndex\out\build\x64-Debug
2> [CMake] – --------------------- Testing
2> [CMake] ---- CMAKE_SOURCE_DIR = C:/multiDimIndex
2> [CMake] – C:/multiDimIndex/gclib/gc.h
2> [CMake] – GCLIB_INCLUDE_DIR-NOTFOUND
2> [CMake] – GCLIB_LIBRARY-NOTFOUND
2> [CMake] === librerías format
2> [CMake] – Configuring done
2> [CMake] – Generating done
2> [CMake] – Build files have been written to: C:/multiDimIndex/out/build/x64-Debug
2> [CMake]
2> Extracted includes paths.
2> Extracted CMake variables.
2> Extracted source files and headers.
2> Extracted code model.
2> CMake generation finished.

Thank in advance!

I am not sure what I am doing wrong to include the gc.h.

image

WTF… after installing the SDKs as suggested here the CMake output is longer.

So with the next CMakeList.txt…

CMakeList.txt : CMake project for multiDimIndex, include source and define

project specific logic here.

project(multiDimIndex)
cmake_minimum_required (VERSION 3.8)
message(STATUS “--------------------- Testing”)

Add source to this project’s executable.

message("---- CMAKE_SOURCE_DIR = " ${CMAKE_SOURCE_DIR})

include_directories(${CMAKE_SOURCE_DIR}/gclib)
link_directories(${CMAKE_SOURCE_DIR}/gclib)
find_path(GCLIB_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/gclib/gc.h)
find_library(GCLIB_LIBRARY “.\gc-lib”)

message (“-- " ${CMAKE_SOURCE_DIR}/gclib/gc.h)
message(”–=== " ${GCLIB_INCLUDE_DIR})
message("–=== " ${GCLIB_LIBRARY})
set(YOUR_LIBRARIES)

if (GCLIB_INCLUDE_DIR AND GCLIB_LIBRARY)
# you may need that if further action in your CMakeLists.txt depends
# on detecting your library
set(GCLIB_FOUND TRUE)

# you may need that if you want to conditionally compile some parts
# of your code depending on library availability
add_definitions(-DHAVE_GCLIB=1)

# those two, you really need
include_directories(${GCLIB_INCLUDE_DIR})
set(YOUR_LIBRARIES ${YOUR_LIBRARIES} ${GCLIB_LIBRARY})

endif ()

configure_file(multiDimIndex.config.h.in multiDimIndex.config.h) #fichero donde poner configuraciones para el proyecto ()

add_library(format format.cpp)

add_executable (multiDimIndex “multiDimIndex.cpp” “multiDimIndex.h” “${CMAKE_SOURCE_DIR}/gclib/gc.h”) #“…/PruebaGC/include/gc.h”) “./gc-lib/gc.h”
#target_link_libraries(multiDimIndex ${YOUR_LIBRARIES})
set(YOUR_LIBRARIES ${YOUR_LIBRARIES} format)
message ("========= libraries = " ${YOUR_LIBRARIES})
target_link_libraries(multiDimIndex ${YOUR_LIBRARIES})
#target_link_libraries(multiDimIndex gc-lib format)

target_include_directories(multiDimIndex PUBLIC
“${PROJECT_BINARY_DIR}”
)

… we obtain:

1> CMake generation started for configuration: ‘x64-Debug’.
1> Command line: “cmd.exe” /c ““C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe” -G “Ninja” -DCMAKE_INSTALL_PREFIX:PATH=“C:\multiDimIndex\out\install\x64-Debug” -DCMAKE_CXX_COMPILER:FILEPATH=“C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/HostX64/x64/cl.exe” -DCMAKE_C_COMPILER:FILEPATH=“C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/HostX64/x64/cl.exe” -DCMAKE_BUILD_TYPE=“Debug” -DCMAKE_MAKE_PROGRAM=“C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe” “C:\multiDimIndex” 2>&1”
1> Working directory: C:\multiDimIndex\out\build\x64-Debug
1> [CMake] – --------------------- Testing
1> [CMake] ---- CMAKE_SOURCE_DIR = C:/multiDimIndex
1> [CMake] – C:/multiDimIndex/gclib/gc.h
1> [CMake] --=== GCLIB_INCLUDE_DIR-NOTFOUND
1> [CMake] --=== GCLIB_LIBRARY-NOTFOUND
1> [CMake] ========= libraries = format
1> [CMake] – Configuring done
1> [CMake] – Generating done
1> [CMake] – Build files have been written to: C:/multiDimIndex
1> [CMake]
1> Extracted includes paths.
1> Extracted CMake variables.
1> Extracted source files and headers.
1> Extracted code model.
1> CMake generation finished.

But still…
image

Ok, I found that CMakeSettings.json is needed. I found it just by chance in VS. After setting there the values it finds the library but it still says unresolved external symbol GC_init.

Now trying with a different CMakeList.txt I obtain a different result (I have added spaces before and after @):

cmake_minimum_required (VERSION 3.8)
project(multiDimIndex)

ADD_DEFINITIONS(-DGC_NOT_DLL)
add_executable (multiDimIndex WIN32 “multiDimIndex.cpp” “multiDimIndex.h” “${CMAKE_SOURCE_DIR}/gclib/gc.h”) #“…/PruebaGC/include/gc.h”) “./gc-lib/gc.h”
TARGET_LINK_LIBRARIES(multiDimIndex gc-lib)

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol “class std::basic_string<char,struct std::char_traits,class std::allocator > __cdecl binary(unsigned __int64)” (?binary@@YA?AV?$basic_string@DU?$char_traits @ D @ std @ @ V?$allocator @ D @ 2 @ @ std @ @ _K @ Z) referenced in function main C:\multiDimIndex\out\build\x64-Debug\multiDimIndex C:\multiDimIndex\out\build\x64-Debug\multiDimIndex.cpp.obj 1
Error LNK2019 unresolved external symbol WinMain referenced in function “int __cdecl invoke_main(void)” (?invoke_main @ @ YAHXZ) C:\multiDimIndex\out\build\x64-Debug\multiDimIndex C:\multiDimIndex\out\build\x64-Debug\MSVCRTD.lib(exe_winmain.obj) 1

ok, it ended working on windows with the next CMakeList.txt:

cmake_minimum_required (VERSION 3.8)
project(multiDimIndex)

add_library(format format.cpp)
ADD_DEFINITIONS(-DGC_NOT_DLL)
add_executable (multiDimIndex “multiDimIndex.cpp” “multiDimIndex.h” “${CMAKE_SOURCE_DIR}/gclib/gc.h”) #“…/PruebaGC/include/gc.h”) “./gc-lib/gc.h”
TARGET_LINK_LIBRARIES(multiDimIndex gc-lib format)