0

I want to add an external compiled dynamic library (here multiple .dll of opencv) to my own .dll project. However, I succeed to compile it, but every time it crashes when I start it.

I look up and learned that compiling and running are not the same with .dll, and it look like my problem because if I put those .dll in the program's root dir, it work well.

This is my Makefile :

TARGET = MyNewLib.dll
CXX = g++.exe

LIB_PATH = PATH/TO/OPENCV/LIB_FOLDER

CXXFLAGS = -std=c++11 -I/$(LIB_PATH)/opencv/include

OPENCV_DIR = $(LIB_PATH)/opencv/x86/mingw/lib
OPENCV_LIBS = -lopencv_core2413 -lopencv_imgproc2413 -lopencv_highgui2413

SOURCES = $(wildcard *.cpp) 
INCLUDES = $(wildcard *.h) $(wildcard /$(LIB_PATH)/opencv/include/opencv2/*.hpp)
OBJECTFILES = $(SOURCES:%.cpp=%.o)


build: $(TARGET)

$(TARGET): $(OBJECTFILES)
    ${LINK.cc} -shared -o $@ ${OBJECTFILES} -L/$(OPENCV_DIR) $(OPENCV_LIBS) -I/$(LIB_PATH)/opencv/include

.cpp.o:
    $(CXX) -MD -MP $(CXXFLAGS) -o $@ -c $<

clean:
    - testprogram.exe -c "unload code" 2> NUL
    $(RM) $(OBJECTFILES) $(TARGET)
    
$(OBJECTFILES): $(INCLUDES)

And my opencv dir is like :

PATH/TO/OPENCV/.

  • include
    • opencv
    • opencv2 {folders and .hpp like opencv.hpp}
  • x86/mingw/
    • bin {libs like libopencv_core2413.dll}
    • lib {libs like libopencv_core2413.dll.a}

I already tried my best with export LD_RUN_PATH or solutions here https://stackoverflow.com/a/34909759/12872793 but I have no idea how link the lib from another folder at running time, can you help me please ?

EDIT ---

What I tried :

TARGET = MyNewLib.dll
CXX = g++.exe

LIB_PATH = PATH/TO/OPENCV/LIB_FOLDER

CXXFLAGS = -std=c++11 -I/$(LIB_PATH)/opencv/include

OPENCV_DIR = $(LIB_PATH)/opencv/x86/mingw/lib
OPENCV_LIBS = -lopencv_core2413 -lopencv_imgproc2413 -lopencv_highgui2413

SOURCES = $(wildcard *.cpp) 
INCLUDES = $(wildcard *.h) $(wildcard /$(LIB_PATH)/opencv/include/opencv2/*.hpp)
OBJECTFILES = $(SOURCES:%.cpp=%.o)


build: $(TARGET)

$(TARGET): $(OBJECTFILES)
    export LD_RUN_PATH=$(OPENCV_DIR)/:/path/to/lib2:/path/to/lib3
    ${LINK.cc} -shared -o $@ ${OBJECTFILES} -L/$(OPENCV_DIR) $(OPENCV_LIBS) -I/$(LIB_PATH)/opencv/include
    export LD_RUN_PATH=/$(LIB_PATH)/opencv/x86/mingw/bin/libopencv_core2413.dll:/$(LIB_PATH)/opencv/x86/mingw/bin/libopencv_imgproc2413.dll:/$(LIB_PATH)/opencv/x86/mingw/bin/libopencv_highgui2413.dll
    ${LINK.cc} -shared -o $@ ${OBJECTFILES} -L/$(OPENCV_DIR) -L/$(LIB_PATH)/opencv/x86/mingw/lib/libopencv_core2413.dll.a -lopencv_core2413 -L/$(LIB_PATH)/opencv/x86/mingw/lib/libopencv_imgproc2413.dll.a -lopencv_imgproc2413  -L/$(LIB_PATH)/opencv/x86/mingw/lib/libopencv_highgui2413.dll.a -lopencv_highgui2413
    
.cpp.o:
    $(CXX) -MD -MP $(CXXFLAGS) -o $@ -c $<

clean:
    - testprogram.exe -c "unload code" 2> NUL
    $(RM) $(OBJECTFILES) $(TARGET)
    
$(OBJECTFILES): $(INCLUDES)

(and same for mingw/bin with .dll)

TARGET = MyNewLib.dll
CXX = g++.exe

LIB_PATH = PATH/TO/OPENCV/LIB_FOLDER

CXXFLAGS = -std=c++11 -I/$(LIB_PATH)/opencv/include

OPENCV_DIR = $(LIB_PATH)/opencv/x86/mingw/lib
OPENCV_LIBS = -lopencv_core2413 -lopencv_imgproc2413 -lopencv_highgui2413 -Wl,-rpath=../$(OPENCV_DIR)

SOURCES = $(wildcard *.cpp) 
INCLUDES = $(wildcard *.h) $(wildcard /$(LIB_PATH)/opencv/include/opencv2/*.hpp)
OBJECTFILES = $(SOURCES:%.cpp=%.o)


build: $(TARGET)

$(TARGET): $(OBJECTFILES)
    ${LINK.cc} -shared -o $@ ${OBJECTFILES} -L/$(OPENCV_DIR) $(OPENCV_LIBS)

.cpp.o:
    $(CXX) -MD -MP $(CXXFLAGS) -o $@ -c $<

clean:
    - testprogram.exe -c "unload code" 2> NUL
    $(RM) $(OBJECTFILES) $(TARGET)
    
$(OBJECTFILES): $(INCLUDES)
Nawem
  • 23
  • 5
  • 2
    ***it work well.*** I assume that means the .dlls are not found during runtime. Set the OS `PATH` environment variable or copy the `.dll` to the same folder as the executable – drescherjm May 24 '23 at 13:46
  • ***I already tried my best with export LD_RUN_PATH or solutions here*** Where did you attempt this? It would not work in a command prompt or powershell or from the file explorer as this is not a MS Windows concept however it may work in a mingw64 terminal but I am not sure. – drescherjm May 24 '23 at 13:51
  • I edit with what I tried and I'm aggry that means the .dlls files are not found during runtime. I don't want to copy them in same folder. How do I set the OS PATH and why if I give reael path to makefile ? – Nawem May 24 '23 at 15:18
  • 2
    The other answer you're looking at is specifically talking about POSIX systems (Linux for example). You are working on Windows. Windows is entirely and completely different than Linux and you generally, and definitely not in this situation, cannot apply POSIX solutions to a Windows system. So things like setting rpath etc. on Windows won't work. A very large proportion of questions and answers on SO are about POSIX systems, not Windows, so when looking for answers be sure to check that. – MadScientist May 24 '23 at 15:44
  • 1
    On Windows, you can read about how DLLs are found here: https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order – MadScientist May 24 '23 at 15:47
  • [https://www.wikihow.com/Change-the-PATH-Environment-Variable-on-Windows](https://www.wikihow.com/Change-the-PATH-Environment-Variable-on-Windows) – drescherjm May 24 '23 at 16:07

0 Answers0