在 Wt 上编译 hello world 的问题

Posted

技术标签:

【中文标题】在 Wt 上编译 hello world 的问题【英文标题】:Issue compiling hello world on Wt 【发布时间】:2011-07-02 15:37:26 【问题描述】:

我尝试使用 Wt 编译一个类似于 hello world 的应用程序,但链接有问题 我使用 Qt creator 和 mingw32 作为编译器

我做错了什么?任何帮助将不胜感激

The process "C:/MinGW32/bin/mingw32-make.exe" exited normally.
Configuration unchanged, skipping qmake step.
Starting: "C:/MinGW32/bin/mingw32-make.exe" -w
mingw32-make: Entering directory `C:/Wt/HelloWt-build-desktop'

C:/MinGW32/bin/mingw32-make -f Makefile.Debug

mingw32-make[1]: Entering directory `C:/Wt/HelloWt-build-desktop'

g++ -c -DNDEBUG -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -D__MINGW32__ -D_WIN32 -DQT_DLL -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -I'../../Qt/2010.05/qt/include/QtCore' -I'../../Qt/2010.05/qt/include/QtNetwork' -I'../../Qt/2010.05/qt/include' -I'../../Wt2/Include' -I'../../boost/include/boost-1_45' -I'../../Qt/2010.05/qt/include/ActiveQt' -I'debug' -I'../../WtTest/HelloWt' -I'.' -I'../../Qt/2010.05/qt/mkspecs/win32-g++' -o debug/main.o ../../WtTest/HelloWt/main.cpp

g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-subsystem,console -mthreads -Wl -o debug/HelloWt.exe debug/main.o  -L'c:/Qt/2010.05/qt/lib' C:\Wt2\lib\libwt.a C:\Wt2\lib\libwthttp.a C:\QtSDK\mingw\lib\libws2_32.a C:\QtSDK\mingw\lib\libwsock32.a C:\boost\lib\libboost_thread-mgw45-mt-d-1_45.a C:\boost\lib\libboost_regex-mgw45-mt-d-1_45.a C:\boost\lib\libboost_date_time-mgw45-mt-d-1_45.a C:\boost\lib\libboost_signals-mgw45-mt-d-1_45.a C:\boost\lib\libboost_system-mgw45-mt-d-1_45.a C:\boost\lib\libboost_program_options-mgw45-mt-d-1_45.a C:\boost\lib\libboost_filesystem-mgw45-mt-d-1_45.a -lQtNetworkd4 -lQtCored4 

mingw32-make[1]: Leaving directory `C:/Wt/HelloWt-build-desktop'

mingw32-make: Leaving directory `C:/Wt/HelloWt-build-desktop'

C:\Wt2\lib\libwthttp.a(WServer.obj): In function `WServer':

C:/wt-3.1.9/src/http/WServer.C:142: undefined reference to `Wt::WAbstractServer::instance_'

C:/wt-3.1.9/src/http/WServer.C:140: undefined reference to `Wt::WAbstractServer::~WAbstractServer()'

C:\Wt2\lib\libwthttp.a(WServer.obj): In function `~HTTPStream':

C:/wt-3.1.9/src/http/HTTPStream.h:16: undefined reference to `Wt::WebStream::~WebStream()'

C:\Wt2\lib\libwthttp.a(WServer.obj): In function `~WServer':

C:/wt-3.1.9/src/http/WServer.C:145: undefined reference to `Wt::WAbstractServer::~WAbstractServer()'

C:\Wt2\lib\libwthttp.a(WServer.obj): In function `~HTTPStream':

C:/wt-3.1.9/src/http/HTTPStream.h:16: undefined reference to `Wt::WebStream::~WebStream()'

C:\Wt2\lib\libwthttp.a(WServer.obj): In function `~WServer':

C:/wt-3.1.9/src/http/WServer.C:145: undefined reference to `Wt::WAbstractServer::~WAbstractServer()'

C:\Wt2\lib\libwthttp.a(HTTPStream.obj): In function `HTTPStream':

C:/wt-3.1.9/src/http/HTTPStream.C:17: undefined reference to `Wt::WebStream::WebStream(bool)'

C:\Wt2\lib\libwthttp.a(HTTPStream.obj): In function `~HTTPStream':

C:/wt-3.1.9/src/http/HTTPStream.h:16: undefined reference to `Wt::WebStream::~WebStream()'

C:/wt-3.1.9/src/http/HTTPStream.h:16: undefined reference to `Wt::WebStream::~WebStream()'

collect2: ld returned 1 exit status

mingw32-make[1]: *** [debug/HelloWt.exe] Error 1

mingw32-make: *** [debug] Error 2

The process "C:/MinGW32/bin/mingw32-make.exe" exited with code %2.
Error while building project HelloWt (target: Desktop)
When executing build step 'Make'

这里是源码

#include <Wt/WApplication>
#include <Wt/WBreak> 
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>



#include <boost/version.hpp>

using namespace Wt;

/*
* A simple hello world application class which demonstrates how to react
* to events, read input, and give feed-back.
*/
class HelloApplication : public WApplication

public:
  HelloApplication(const WEnvironment& env);

private:
  WLineEdit *nameEdit_;
  WText *greeting_;

  void greet();
;

/*
* The env argument contains information about the new session, and
* the initial request. It must be passed to the WApplication
* constructor so it is typically also an argument for your custom
* application constructor.
*/
HelloApplication::HelloApplication(const WEnvironment& env)
  : WApplication(env)

  setTitle("Hello world");                               // application title

  root()->addWidget(new WText("Your name, please ? "));  // show some text
  nameEdit_ = new WLineEdit(root());                     // allow text input
  nameEdit_->setFocus();                                 // give focus

  WPushButton *b = new WPushButton("Greet me.", root()); // create a button
  b->setMargin(5, Left);                                 // add 5 pixels margin

  root()->addWidget(new WBreak());                       // insert a line break

  greeting_ = new WText(root());                         // empty text

  /*
   * Connect signals with slots
   *
   * - simple Wt-way
   */
  b->clicked().connect(this, &HelloApplication::greet);

  /*
   * - using an arbitrary function object (binding values with boost::bind())
   */
  nameEdit_->enterPressed().connect
    (boost::bind(&HelloApplication::greet, this));


void HelloApplication::greet()

  /*
   * Update the text, using text input into the nameEdit_ field.
   */
   greeting_->setText("Hello there, " + nameEdit_->text());


WApplication *createApplication(const WEnvironment& env)

  /*
   * You could read information from the environment to decide whether
   * the user has permission to start a new application
   */
  return new HelloApplication(env);


int main(int argc, char **argv)

  /*
  * Your main method may set up some shared resources, but should then
  * start the server application (FastCGI or httpd) that starts listening
  * for requests, and handles all of the application life cycles.
  *
  * The last argument to WRun specifies the function that will instantiate
  * new application objects. That function is executed when a new user surfs
  * to the Wt application, and after the library has negotiated browser
  * support. The function should return a newly instantiated application
  * object.
  */
  return WRun(argc, argv, &createApplication);

【问题讨论】:

你也可以发布你的源代码吗?" 【参考方案1】:

您可能需要在命令行中颠倒 libwt.a 和 libwthttp.a 的顺序。参数的顺序有时对 GNU 链接器很重要。

【讨论】:

【参考方案2】:

您正在尝试将您的应用程序与 QtCore、QtNetwork 和 ActiveQt 库链接,而不是与 Wt 库链接。

据我所知,您正在取消 qmake 然后 make。 如果这样做,请检查您的项目配置文件,并与使用 qmake 的 Wt 的 Hello World 示例进行比较:

QT       -= core
QT       -= gui

TARGET = CppHelloWtQtCreatorUbuntu
LIBS += -L/usr/lib -lwt -lwthttp
QMAKE_CXXFLAGS += -DNDEBUG
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
    wittyapplication.cpp

HEADERS += \
    wittyapplication.h

正如您在此处看到的,所有与 Qt 库的链接都被删除并添加了 wt 和 wthttp(二进制女巫是一个独立的网络服务器)库。 还要确保您可以通过 INCLUDE 环境变量访问 Wt 头文件,并且可以通过 PATH 环境变量访问 Wt 库。

【讨论】:

以上是关于在 Wt 上编译 hello world 的问题的主要内容,如果未能解决你的问题,请参考以下文章

通过命令行在 OS X 上编译简单的 Hello World 程序

无法让 Qt Hello World 工作

docker hello world

GCC 在尝试在 OSX 10.6 上编译 64 位代码时死了

如何让 OpenGL Hello World 为 OSX 工作?

C++/G++ hello world 应用程序问题,可能是编译