我的源代码:
#include <qjson/serializer.h>
#include <QDebug>
int main() {
QVariantMap numbers;
numbers.insert("one", 1);
numbers.insert("two", 2);
QJson::Serializer serializer;
qDebug() << serializer.serialize(numbers);
return 0;
}
我的项目文件:
TEMPLATE = app
LIBS += -lqjson
SOURCES += main.cpp
如果我在Debian上构建(并运行)它,这很有效:
$ qmake && make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -o main.o main.cpp
g++ -m64 -Wl,-O1 -o jsongen main.o -L/usr/lib/x86_64-linux-gnu -lqjson -lQtGui -lQtCore -lpthread
$ ./jsongen
"{ "one" : 1, "two" : 2 }"
如果我为Windows构建它会失败:
$ i686-pc-mingw32-qmake && make
make -f Makefile.Release
make[1]: Entering directory `/home/wena/src/dcp/apps/j1939/winapp/play'
i686-pc-mingw32-g++ -c -pipe -fno-keep-inline-dllexport -O2 -frtti -fexceptions -mthreads -Wall -Wextra -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -I'/home/wena/src/mxe/usr/i686-pc-mingw32/qt/include/QtCore' -I'/home/wena/src/mxe/usr/i686-pc-mingw32/qt/include/QtGui' -I'/home/wena/src/mxe/usr/i686-pc-mingw32/qt/include' -I'/home/wena/src/mxe/usr/i686-pc-mingw32/qt/include/ActiveQt' -I'release' -I'/home/wena/src/mxe/usr/i686-pc-mingw32/qt/mkspecs/win32-g++-4.6' -o release/main.o main.cpp
i686-pc-mingw32-g++ -Wl,-s -mthreads -Wl,-subsystem,windows -o release/jsongen.exe release/main.o -L'/home/wena/src/mxe/usr/i686-pc-mingw32/qt/lib' -L'/home/wena/src/mxe/usr/i686-pc-mingw32/qt/lib' -lmingw32 -lqtmain -lqjson -lQtGui -lgdi32 -lcomdlg32 -loleaut32 -limm32 -lwinmm -lwinspool -lmsimg32 -lmng -llcms -L/home/wena/src/mxe/usr/i686-pc-mingw32/lib -ltiff -llzma -ljpeg -lQtCore -lpng -lole32 -luuid -lws2_32 -ladvapi32 -lshell32 -luser32 -lkernel32 -lz
release/main.o:main.cpp:(.text.startup+0xcc): undefined reference to `_imp___ZN5QJson10SerializerC1Ev'
release/main.o:main.cpp:(.text.startup+0xf2): undefined reference to `_imp___ZN5QJson10Serializer9serializeERK8QVariant'
/home/wena/src/mxe/usr/lib/gcc/i686-pc-mingw32/4.7.2/../../../../i686-pc-mingw32/bin/ld: release/main.o: bad reloc address 0xf2 in section `.text.startup'
/home/wena/src/mxe/usr/lib/gcc/i686-pc-mingw32/4.7.2/../../../../i686-pc-mingw32/bin/ld: final link failed: Invalid operation
collect2: error: ld returned 1 exit status
make[1]: *** [release/jsongen.exe] Error 1
make[1]: Leaving directory `/home/wena/src/dcp/apps/j1939/winapp/play'
make: *** [release] Error 2
请注意交叉构建环境,例如 Qt (4.8.3), QJson (0.7.1)和i686-pc-mingw32-qmake
是在MXE的帮助下构建的:
$ cd <path to mxe root>
$ make qjson
(wait about an hour on a recent machine with decent internet connection)
$ export PATH=<path to mxe root>:$PATH
答案 0 :(得分:0)
我选择了更简单的解决方案,将qt-json(一种更简单的替代方案)放入我的存储库中。所需的改变并不多:
$ cat main.cpp
#include "qt-json/json.h"
#include <QDebug>
int main() {
QVariantMap numbers;
numbers.insert("one", 1);
numbers.insert("two", 2);
QtJson::Json json;
qDebug() << json.serialize(numbers);
return 0;
}
$ cat jsongen.pro
TEMPLATE = app
SOURCES += main.cpp
HEADERS += qt-json/json.h
SOURCES += qt-json/json.cpp