Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

qmake - Linker doesn't use a default runtime library when linking together libraries only (no objects)

I want the users to be able to re-link my Qt-using application to their own build of Qt, without being forced to rebuild all of the sources. This could be used for LGPL compliance, for example. To do this, I need to provide object files for all of my sources. To make it easy, using qmake, I've partitioned the project internally into:

  1. A static library project that contains objects for all of the source files, including the file that has int main(int, char**).

  2. An application project that links the static library above with Qt. Qt may be either a static library or dynamic. There are no source files for this project.

I then distribute the static library (.lib) and the application project file so that anyone can relink the application with their own version of Qt, in whichever fashion they prefer (either statically linked Qt or dynamically linked Qt), as long as they have the necessary version of MSVC.

I'm doing the build under both MSVC 2008 (Qt 4) and 2012 (Qt 5). The makefiles are generated by qmake.

The problem is that the linking fails when building the application project.

LINK : error LNK2001: unresolved external symbol _WinMainCRTStartup

As soon as I add a dummy source file dummy.cpp to the application project, the linking succeeds. Is there a way of avoiding this workaround?

//dummy.cpp (this is the entire source)
int dummy;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It turns out that the linker isn't clever enough to figure out what default runtime library is needed for the executable if only static libraries are given to link, with no discrete object files. This can be corroborated by asking the linker to be verbose in the .pro file:

win32-msvc*: QMAKE_LFLAGS += /VERBOSE /VERBOSE:LIB /VERBOSE:REF

When the dummy file is present in the application project, the linker lists the following default libraries:

Processed /DEFAULTLIB:msvcprt
Processed /DEFAULTLIB:MSVCRT
Processed /DEFAULTLIB:OLDNAMES
Processed /DEFAULTLIB:uuid.lib

Without the dummy file, no default libraries are chosen by the linker at all. It is then unable to find the entry point, since the C runtime is not linked in.

Adding the relevant C runtime library is sufficient to link the application. In the application project file, one adds:

win32-msvc*:CONFIG(release, debug|release): QMAKE_LFLAGS += /DEFAULTLIB:msvcrt
win32-msvc*:CONFIG(debug, debug|release): QMAKE_LFLAGS += /DEFAULTLIB:msvcrtd

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...