Difficulty importing a library

Hi, as will be apparent I am new to using CMake. I’m using CLion in Windows with the visual studio 2019 toolchain, and I’m trying to import a library to use and I’m having trouble.

The library is copied to the top of the project, in the folder next to main. The project structure looks like this:

CMakeLists.txt
main.cpp
testClass/testClass.h
testClass/testClass.cpp
libName/DLL/THE_LIB.dll
libName/Lib/THE_LIB.lib
libName/Include/THE_LIB.h
libName/Include/VariousOthers1.h
libName/Include/VariousOthers2.h

The Cmake file looks like this:

cmake_minimum_required(VERSION 3.30)
project(untitled)

set(CMAKE_CXX_STANDARD 20)

add_executable(untitled main.cpp
    testClass/testClass.cpp
    testClass/testClass.h)

target_link_libraries(untitled C:/path/to/lib/libName/Lib/THE_LIB.lib)

main:

#include "testClass/testClass.h"

#include "libName/Include/THE_LIB.h"
#include "libName/Include/VariousOthers1.h"
#include "libName/Include/VariousOthers2.h"

int main() {
    libNameClass instance = libNameClass();
    testClass greeter = testClass();
    greeter.greet();
    return 0;
}

If I comment out the libNameClass and the #includes relating to it, it works fine (testClass prints hello world). If I leave them in, I get an error and a warning:

“error LNK2019: unresolved external symbol” relating to the libNameClass
“warning LNK4272: library machine type ‘x86’ conflicts with target machine type ‘x64’”

Have I made an error with my CMake file, or is the error due to the warning, and is there an obvious way to get around the warning? I would have thought 64 bit machines oculd run 32 bit libraries (and I’m not sure why this warning is coming up, as the library is relatively recent).

Thanks

You are linking a 32bit library but you are building a 64bit program. You can run x86 programs on a x64 Windows but the parts of a program must match.

1 Like

Ok, so if the lilbrary were 64 bit then the CMake commands I’ve used should work, I’ve just got the wrong version of a library? That’s good to know, thank you for your help!

You should also not use Windows-style path in cmake.

1 Like

In favour of:

${PROJECT_SOURCE_DIR}/libName/Lib/THE_LIB.lib

presumably? I had been trying to use that, but either way it doesn’t work for the more fundamental x86 vs x64 reason you outlined above. Thanks again for your help!