Handmade Hero»Forums»Code
Gon
7 posts
None
How to link libraries in Cmake?
Edited by Gon on
I'm sorry if this has been asked before. In day 01 and 02 of Week 1, Casey fixes undefined symbols compiling problems by adding these (user32.lib, gdi32.lib) to the build.bat file. How do you fix this in cmake?
Mārtiņš Možeiko
2559 posts / 2 projects
How to link libraries in Cmake?
Edited by Mārtiņš Možeiko on
The correct question is how link with libraries in cmake. You do that with target_link_libraries.
After add_executable(ProjectName ...) add following:
1
target_link_libraries(ProjectName user32.lib gdi32.lib)
Gon
7 posts
None
How to link libraries in Cmake?
Edited by Gon on
Thanks very much for a quick answer but I still can't build the project and here's the error message.

/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/../../../../x86_64-pc-cygwin/bin/ld: cannot find -luser32.lib

Note:
I tried to use absolute path ../windows/system/user32.lib and it still does not work.
Mārtiņš Možeiko
2559 posts / 2 projects
How to link libraries in Cmake?
Edited by Mārtiņš Možeiko on
Try just "user32" without any .lib suffix.

I'm not sure what you mean by "absolute path" for user32.lib. You wrote relative path. And it's been a while since I used cygwin, but as far as I remember it uses .a or .dll.a suffix for import libraries. No .lib - that is MSVC specific suffix. Although import libraries are the same for binutils and MSVC linker.

Btw I strongly recommend to get rid of cygwin, and instead use msys2 which provides native mingw port for gcc compiler.
Gon
7 posts
None
How to link libraries in Cmake?
Edited by Gon on
user32 without .lib works. Also thanks for correcting me on absolute vs relative path.
Gon
7 posts
None
How to link libraries in Cmake?
I'm testing CLion IDE and I only have two options which are either MinGW or Cygwin.
Mārtiņš Možeiko
2559 posts / 2 projects
How to link libraries in Cmake?
Edited by Mārtiņš Možeiko on
Yeah, choose mingw over cygwin. Cygwin produces binaries that depend on cygwin runtime (which provides POSIX compatibility). With mingw you'll get native binaries that depend only on system dll files. Unless you are creating/using some software that depend on open-source libraries that doesn't compile under mingw, there is absolutely no reason to use cygwin.