2

I am new to QT Creator coming from Visual Studio. I have a session with two projects in it. One is a DLL with some classes that I intend to use for other purposes. The other is an executable console app that uses some of the classes from the DLL.

I currently have these two projects side by side in QT Creator. I can include the header files from the DLL in my EXE project using relative paths "../MyPrject/header.h". But how do I get QT Creator to link and then copy the DLL into the executable debug folder for debugging.

Am I doing this all wrong? Is there a better way? If it includes adding code to the .pro file, please include a link so that I can learn more.

ismail
  • 46,010
  • 9
  • 86
  • 95
narmaps
  • 373
  • 5
  • 16

3 Answers3

0

You should make some dependencies between this projects.

  1. opening both projects - you have done.
  2. on editor view, right click on exe-project and select add library...
  3. follow creator hints to add it.

2nd option: you can make subprojects. follow QtCreator: Creating Projects from documentation (help view in Qt Creator)

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77
0

GwyenBleidD provided a good starting point for including DLLs.

enter image description here

I however, have made a habit out of modifying the .pro file directly here and honestly I prefer to modify the .pro file in the event that something goes haywire.

Suppose I wanted to use the winsock DLL.

In the .pro file, I'd first specify the .dll's corresponding .lib file:

# WinSock2 library (ws2_32.lib file)
LIBS += -lws2_32

# Path to the WinSock2 library
LIBS += -L"c:/mylibraries/"

Additionally, you'll need to specify the include path to the header files here:

INCLUDEPATH += "c:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/INCLUDE"

Thirdly, in my code I'd have to make sure to include the headers for it:

// I ASSUME it'll be found under something like the 
// Visual Studio/VC/INCLUDE directory mentioned above.
#include <winsock2.h>  

Lastly, you need to ensure that your application can find the .dll file, typically pointed to using the %PATH% environment variable.

With regards to your setup, I'd make sure that your sub-projects are configured so that the library compiles FIRST (obviously). And then ensure that the LIBS variable in your .Pro project points correctly to your .lib destination according to the build configuration (debug|release).

Qt's PRO (qmake) isn't as terrible as some make it out to be. Just give it a solid half-hour to an hour and you'll get the hang of it. I assume though that you have a solid understanding of libs and DLLs and what not.

http://qt-project.org/doc/qt-5.0/qtdoc/qmake-manual.html

Community
  • 1
  • 1
Son-Huy Pham
  • 1,899
  • 18
  • 19
  • OK, so I started a new project based on the SUBDIRS template and then recreated my two subprojects. I have everything compiled. I have also added the DLL library as an "Internal" library (and not as "external" because I figured it was in my build tree). I have a class declared in my DLL and added the "export" specifier. I am having trouble using it in the EXE project. It complains about unresolved external symbol. I tried adding "using MyClass;" at the top of main.cpp, but this didn't help. What do I need in place before I can use a class from my DLL project in the executable? Thanks! – narmaps Sep 17 '13 at 19:47
0

The right way is to switch on CMake based project and keep exe and dll within one root project. The main benefit of this decision is IDE independent approach: you can use Qt Creator, CLion, Visual Studio without any changes in project definition. As the start point consider to see the example project https://github.com/anatoly-spb/cmake_exe_dll

AnatolyS
  • 4,249
  • 18
  • 28