Solved Looking for good explanation of creation process of Static Lib and it's Test App
-
Hi!
Could you please explain about the creation of static lib with a test app for both OS like Linux and Windows with the right relative paths? I'm in a mess. What is work fine in Linux, cannot be recompiled and work in Windows. There is magic with paths, shadow builds and etc. -
Hi,
Can you show your .pro files ?
-
I guess you need to provide a little bit more information.
- What make toolkit are you using?
- Is the lib in the same project as the executable
- What exactly is the problem you have?
-
@SGaist , @gde23
test app pro file:QT -= gui CONFIG += c++11 console CONFIG -= app_bundle DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target win32 { CONFIG(release, debug|release){ LIBS += -L$$PWD/../project1/cpp/dll/mylib/release/ -lmylib INCLUDEPATH += $$PWD/../project1/cpp/dll/mylib/release DEPENDPATH += $$PWD/../project1/cpp/dll/mylib/release } CONFIG(debug, debug|release){ LIBS += -L$$PWD/../project1/cpp/dll/mylib/debug/ -lmylib INCLUDEPATH += $$PWD/../project1/cpp/dll/mylib/debug DEPENDPATH += $$PWD/../project1/cpp/dll/mylib/debug } } unix { LIBS += -L$$PWD/../project1/cpp/dll/mylib/ -lmylib INCLUDEPATH += $$PWD/../project1/cpp/dll/mylib DEPENDPATH += $$PWD/../project1/cpp/dll/mylib }
Lib Pro file:
QT -= gui TARGET = GET_INF_HUB TEMPLATE = lib DEFINES += GET_INF_HUB_LIBRARY DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ mylib.cpp HEADERS += \ mylib.h \ mylib_global.h unix { target.path = /usr/lib INSTALLS += target }
Static lib added to test app like an external lib.
In Linux test app starts without problems.
In Windows, first of all, I need to uncheck Shadow build in test and lib apps... And after compilation there is an error msg:
main.obj:-1: error: LNK2019: unresolved external symbol __imp__Start referenced in function _main
I have cleaned tmp files and folders... But there is no effect... -
First thing: are you sure your library is properly statically built on Windows ?
If not, then I would guess you are not properly exporting the symbols from that library hence the missing symbol.
-
@SGaist said in Looking for good explanation of creation process of Static Lib and it's Test App:
First thing: are you sure your library is properly statically built on Windows ?
How I should check this?
-
Are you properly setting the static flag in your library project ? Do you get a .dll when building on Windows ?
-
@SGaist said in Looking for good explanation of creation process of Static Lib and it's Test App:
Are you properly setting the static flag in your library project ? Do you get a .dll when building on Windows ?
Yes. Dll and Lib files are created after building lib.
-
Then your library is not build statically and you are not properly exporting symbols.
For that second part see here.
-
@SGaist thanks. I will recode my lib.
I there any way to debug dynamic/static lib?
-
Sure, use a debug build of your library.
-
@SGaist ones more Q:
But how the same project works in Linux? -
Because the symbol exportation does not work the same on Linux.
-
When you have a library under windows you need to explicitly export the symbols.
On linux it is not necessary because the symbols are managed in another way.Typically this is done using a preprocessor define that you add at any function / class that you wanna export like (just look it up on stackoverflow)
#ifdef WIN32 #define DECL_EXPORT __declspec(dllexport) #define DECL_IMPORT __declspec(dllimport) #else #define DECL_EXPORT #define DECL_IMPORT #endif
-
@gde23 said in Looking for good explanation of creation process of Static Lib and it's Test App:
When you have a library under windows you need to explicitly export the symbols.
On linux it is not necessary because the symbols are managed in another way.Typically this is done using a preprocessor define that you add at any function / class that you wanna export like (just look it up on stackoverflow)
#ifdef WIN32 #define DECL_EXPORT __declspec(dllexport) #define DECL_IMPORT __declspec(dllimport) #else #define DECL_EXPORT #define DECL_IMPORT #endif
That is incomplete, The macro to use depend whether you are building the library or using it. See the dedicated chapter in Qt's documentation for a clean way to handle that.
-
-
-
@gde23 said in Looking for good explanation of creation process of Static Lib and it's Test App:
@SGaist You are right. I forgot to mention that part.
@sitesv The dll will be created also if you do not export the symbols. But the exe will not know about the function/class inside the dll so it will not find them.
Did you include the defines as in the description SGaist posted?Yes. I checked it. Could you view my code by this link?
UPD: I inserted the line "extern int test();" to main.cpp. And voila!