0

I have problem runnig my c++ code in clion, using cmake. This is example code

#include <shlwapi.h> 

int main() {
    StrDupA(nullptr);
    return 0;
}

And there is an exception:

cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.4\bin\mingw\bin\G__~1.EXE -g -static-libgcc -static-libstdc++ -static CMakeFiles/untitled.dir/main.cpp.obj -o untitled.exe -Wl,--out-implib,libuntitled.dll.a -Wl,--major-image-version,0,--minor-image-version,0  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Program Files\JetBrains\CLion 2022.2.4\bin\mingw\bin/ld.exe: CMakeFiles/untitled.dir/main.cpp.obj:C:/Users/Adm/CLionProjects/untitled/main.cpp:8: undefined reference to `__imp_StrDupA'

CMakeList:

cmake_minimum_required(VERSION 3.23)
project(untitled)
#
set(CMAKE_CXX_STANDARD 11)
add_executable(untitled main.cpp)

It seems like linker doesnt add standard mingw libraries. I have .h files included automatically by clion but there is no source files linked to run code. Do somebody have idea what is going on? How CMakeList look like to include standard libraries. Same situation is with windows libraries from directory C:/Windows/System32/... e.g C:\Windows\System32\nsi.dll

  • You just need to add [`-lshlwapi`](https://learn.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-strdupa) to the linker flags. Different WinAPI functions require linking different libraries, but you don't need to worry about the contents of `C:\Windows` or anything like that; all those libraries are shipped with MinGW. – HolyBlackCat Oct 13 '22 at 13:01
  • 1
    [target_link_libraries](https://cmake.org/cmake/help/latest/command/target_link_libraries.html) to link with cmake e.g. `target_link_libraries(untitled shlwapi)` – dewaffled Oct 13 '22 at 13:08

0 Answers0