0

I'm building a small C library, which I'm using in C#. So far everything is working, but as soon as I add std::vector to my code, running it I get the following error:

Unhandled exception. System.DllNotFoundException: Unable to load DLL 'myLib' or one of its dependencies: The specified module could not be found. (0x8007007E)

Removing the vector and import makes it work again. This is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.25)

project(myLib)

set(CMAKE_CXX_STANDARD  20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v -static-libgcc -static-libstdc++")    

add_subdirectory("portaudio")

add_library(karaokeMicLib SHARED library.cpp PortAudioWrapper.cpp PortAudioWrapper.h)

target_link_libraries(myLib portaudio)
add_dependencies(myLib portaudio)

Setting the flags should make it work from what I can gather, but the dependencies app says that libstdc++-6.dll is missing. I'm also using jetbrains clion. Any help to make the standard library work is greatly appreciated!

enter image description here

cboe
  • 469
  • 1
  • 9
  • 25
  • Did you put `libstdc++-6.dll` in the same folder as your executable? – drescherjm May 03 '23 at 16:07
  • Where is `libstdc++-6.dll` on your system? How are you making it available to your program? This is the missing part. Your code is building correctly, it's only when you run your code that you get the problems. That has nothing to do with CMake. – john May 03 '23 at 16:14
  • @drescherjm I'm building a library, not an executable and I'm using that in a console application. You think it would work if I put it there? Also, isn't the point of -static-libstdc++ flag that I don't need to do this? – cboe May 03 '23 at 16:14
  • @john shouldn't -static-libstdc++ statically link the std library into my library? Or am I missunderstanding it? – cboe May 03 '23 at 16:15
  • @cboe Sorry but I missed that flag. However my understanding is that you must use the `-static` flag as well as the `-static-libstdc++` flag, See [here](https://stackoverflow.com/questions/70633262/how-can-i-fix-libstdc-6-dll-not-found-error-in-my-c-program) – john May 03 '23 at 16:17
  • 3
    `-static-libgcc` is a **linker** flag, but variable `CMAKE_CXX_FLAGS` contains **compiler** flags. Linker flags could be passed via `target_link_libraries` command, like in [that answer](https://stackoverflow.com/a/38696028/3440745) to the duplicate question. – Tsyvarev May 03 '23 at 16:21
  • @cboe In any case it's clear from the dependencies that this flag is not working. It might be worth looking at the build log from CLion to see what flags are actually getting passed to the linker – john May 03 '23 at 16:21
  • Thanks everyone, I've inserted this at the end and now it works: set(CMAKE_CXX_FLAGS " -static") target_link_libraries(karaokeMicLib -static-libgcc -static-libstdc++) i can't say i fully understand but i think it's all i need :D – cboe May 03 '23 at 18:04

0 Answers0