How to link libraries to a project on visual studio?

爷,独闯天下 提交于 2019-12-13 03:15:49

问题


I need to create a solution on VS Express 2017 with two self made dll, LIB_COMMON and LIB_VIEW, a bin project TEST_VIEW containing the main.cpp and using Qt. I have everything needed in my folder and set the environnement variables while launching the solution with a batch file.

But I have two problems. First, I can't manage to link Qt with the projects to use . Secondly, in the properties, the only I found to link my dll project was to add by hand every obj file (yes, obj files, not lib...) needed like in this example. (LIB_VIEW contains the header and cpp files VIEW_FACTORY, VIEW_View, VIEW_IView.

linker -> entry -> additional dependencies -> $(LIB_VIEW)\Obj\$(Platform)\$(Configuration)\VIEW_FACTORY;$(LIB_VIEW)\Obj\$(Platform)\$(Configuration)\VIEW_IView;$(LIB_VIEW)\Obj\$(Platform)\$(Configuration)\VIEW_View;kernel32.lib;user32.lib;gdi32.lib;winspool32.lib      etc.

(as the path for the obj files is : SOLUTION_NAME/LIB_VIEW/Obj/x64/Debug/ for the lib file : SOLUTION_NAME/LIB_VIEW/Lib/x64/Debug/ for the includes : SOLUTION_NAME/LIB_VIEW/Inc/ )

This is awful and when I can't link the Qt libraries like that. Here are some more details

BATCH FILE

set PROJECT_HOME=%~dp0
set PROJECT_TOOLS=%PROJECT_HOME%\Tools

set LIB_TOOLS=%PROJECT_HOME%\LIB_COMMON

echo "----------------------------" 
echo "--- SET_ENV_QT" 
echo "----------------------------" 

set QT_HOME==%PROJECT_TOOLS%\Qt\5.12.0\x64\5.12.0\msvc2017_64

set QT_BIN=%QT_HOME%\bin
set QT_LIB=%QT_HOME%\lib
set QT_INC=%QT_HOME%\include
set QT_QML=%QT_HOME%\qml

echo "----------------------------" 
echo "--- SET_ENV_VISUAL" 
echo "----------------------------"

set LIB_COMMON=%PROJECT_HOME%\LIB_COMMON\
set LIB_VIEW=%PROJECT_HOME%\LIB_VIEW\


set PATH=%OSPL_BIN%;%OSPL_LIB%;%PATH%

echo "----------------------------" 
echo "--- LAUNCH_SLN" 
echo "----------------------------"

"%PROJECT_HOME%\SOLUTION_NAME.sln"

As I said earlier, I found a way to include my dll in the main project : in the properties of TEST_VIEW :

C/C++ -> General -> Additionnal Include directories -> $(LIB_COMMON)/Inc;$(LIB_VIEW)/Inc;$(AdditionalIncludeDirectories);$(QtGui);
linker -> entry -> additional dependencies -> $(LIB_VIEW)\Obj\$(Platform)\$(Configuration)\VIEW_FACTORY;$(LIB_VIEW)\Obj\$(Platform)\$(Configuration)\VIEW_IView;$(LIB_VIEW)\Obj\$(Platform)\$(Configuration)\VIEW_View;kernel32.lib;user32.lib;gdi32.lib;winspool32.lib      etc.

Of course I can't include any Qt file, they can't be found.

What should I change in the properties ?

how can I include sucessfully QGuiApplication ?


回答1:


Edit : I kept adding the obj files of my personnal dll in the linker and added some Qt lib there too.

One of my problems was that the qt dll's were stored in a specific folder, so the application couldn't reach them. After adding the header files by specifying the path Qt/include and all the libs needed in the properties, I made a batch file that add the path to the dll folder to the PATH of my exe file.

set PROJECT_HOME=%~dp0..\..\..\..\
set PROJECT_TOOLS=%PROJECT_HOME%\Tools

set QT_HOME=%PROJECT_TOOLS%\Qt\5.12.0\x64\5.12.0\msvc2017_64

set QT_BIN=%QT_HOME%\bin
set QT_LIB=%QT_HOME%\lib
set QT_INC=%QT_HOME%\include
set QT_QML=%QT_HOME%\qml
set QT_PLATFORM=%QT_HOME%\plugins\platforms

set QML_IMPORT_PATH=%QT_HOME%\qml\QtQuick;%QtHome%\qml\QtQuick2;%QML_IMPORT_PATH%
set PATH=%QT_BIN%;%QT_INC%;%QT_PLATFORMS%;%PATH%

TEST_VIEW3.exe

pause

Oh. And don't forget to check the path to Qt. It was wrong so I couldn't include succesfully QGuiApplication within the linker...

Now it's okay. I still have another error but this step is done. i hope it will be usefull for someone.



来源:https://stackoverflow.com/questions/55809004/how-to-link-libraries-to-a-project-on-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!