Include an assembly *.o file into a Qt C++ project - c++

I have a Qt C++ Project.
System
Windows XP
QtCreator and QtSDK newest
C++
MinGW/gcc/g++
For performance reasons I need some functions to be done in assembly. My Project is mainly Qt and c++ but I need to link my C++ code to some assembly I wrote. I have an assembly *.o file but I can't access my assembly function from my c++ code. I complied the assembly calc.o file with nasm.
Simplified example.
PRO FILE
QT += core
QT -= gui
TARGET = Test_asm
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
LIBS += C:/Users/David/Test_asm/calc.o
SOURCES += main.cpp
MAIN.CPP
#include <QtCore/QCoreApplication>
#include <QTextStream>
extern "C" int five();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextStream qout(stdout);
int f = five();
qout << "five: " << f << endl;
return a.exec();
}
ERROR
main.cpp:11: undefined reference to `five'
collect2: ld returned 1 exit status
TRY #1 - Karlphillip's way (Does not work on Windows)
Directory of C:\x
16/03/2012 03:09 PM <DIR> .
16/03/2012 03:09 PM <DIR> ..
16/03/2012 03:07 PM 82 calc.pro
16/03/2012 03:10 PM 178 calc.S
16/03/2012 03:10 PM 164 main.c
3 File(s) 424 bytes
2 Dir(s) 78,905,851,904 bytes free
C:\x>qmake
C:\x>make
'make' is not recognized as an internal or external command,
operable program or batch file.
C:\x>mingw32-make
mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/x'
gcc -c -g -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -I"..\QtSDK\Desktop\Qt\4.7.3\mi
ngw\mkspecs\default" -o debug\main.o main.c
gcc -c -g -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -I"..\QtSDK\Desktop\Qt\4.7.3\mi
ngw\mkspecs\default" -o debug\calc.o calc.S
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-rel
oc -Wl,-subsystem,console -mthreads -Wl -o debug\calc.exe debug/main.o debug/cal
c.o
debug/main.o: In function `main':
C:\x/main.c:9: undefined reference to `helloasm'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\calc.exe] Error 1
mingw32-make[1]: Leaving directory `C:/x'
mingw32-make: *** [debug] Error 2

The best way that I found to work with assembly in QT Creator, it was to configure the .pro file. I added the nasm compiler to .pro file and some flags. You can change these flags if you need it. But with this configuration works well for me
example.pro
QMAKE_EXTRA_COMPILERS += nasm
NASMEXTRAFLAGS = -f elf64 -g -F dwarf
OTHER_FILES += $$NASM_SOURCES
nasm.output = ${QMAKE_FILE_BASE}.o
nasm.commands = nasm $$NASMEXTRAFLAGS -o ${QMAKE_FILE_BASE}.o ${QMAKE_FILE_NAME}
nasm.input = NASM_SOURCES
Finishing, you have to assign to NASM_SOURCES variable all the ASM files, in my case it was just one and it called arregloAsm.asm
NASM_SOURCES += arregloAsm.asm
I hope that all this works for you too!

Same thing as before:
calc.S:
.data
hello:
.string "Hello World\n"
.globl helloasm
helloasm:
movl $4, %eax
movl $1, %ebx
movl $hello,%ecx
movl $12,%edx
int $0x80
ret
main.c:
#include <stdio.h>
void helloasm();
int main()
{
printf("* Calling ASM function:\n");
helloasm();
printf("* Done!\n");
return 0;
}
calc.pro:
TEMPLATE = app
CONFIG += console
CONFIG -= qt
SOURCES += main.c \
calc.S
After putting all these files within the same directory, execute:
qmake
make
to verify the output:
$ ./calc
* Calling ASM function:
Hello World
* Done!

Why didn't you add calc.o to your sources?

I first did what was mentioned earlier:
testasm.pro
QT += core
QT -= gui
CONFIG += c++11
TARGET = testasm
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
QMAKE_EXTRA_COMPILERS += nasm
NASMEXTRAFLAGS = -f elf64 -g -F dwarf
OTHER_FILES += $$NASM_SOURCES
nasm.output = ${QMAKE_FILE_BASE}.o
nasm.commands = nasm $$NASMEXTRAFLAGS -o ${QMAKE_FILE_BASE}.o ${QMAKE_FILE_NAME}
nasm.input = NASM_SOURCES
NASM_SOURCES += test.asm
main.cpp
#include <QCoreApplication>
extern "C" void test();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
test();
return a.exec();
}
test.asm
global test
bits 64
section .text
test:
mov rax,0
ret
In the menu Build click build all and the application should be abel to run. It's a empty terminal in this example but on my system it works.
I was looking for a solution and thanks to this post, I finally have one.
Just remember to add
extern "C" void test();
to your main.cpp file. Notice "C" in the line because that was my biggest error.
(In case of language errors: I'm no native English speaker, if you like please correct the text)

Related

Adding ObjectBox to Qt application

I have just tried to add ObjectBox to an empty qt application, but somehow I do not get any further.
I tried to follow the instructions: https://cpp.objectbox.io/installation
I took the win x64 lib from here: https://github.com/objectbox/objectbox-c/releases/tag/v0.17.0
I am always getting undefined reference errors and I do not know why. I guess there must be something wrong with linking in der .pro file?
Thats my .pro file (just edited the last 4 lines):
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
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/objectbox/lib/ -lobjectbox
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/objectbox/lib/ -lobjectbox
INCLUDEPATH += $$PWD/objectbox/include
DEPENDPATH += $$PWD/objectbox/include
And here ist my main.cpp:
#include <QCoreApplication>
#include "objectbox.hpp"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
printf("Using ObjectBox version %s\n", obx_version_string());
return a.exec();
}
Compiler says the following:
C:/Qt/Tools/mingw730_64/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'C:/temp/obxTut/ObjectBoxTutorial'
g++ -c -fno-keep-inline-dllexport -g -std=gnu++11 -Wall -W -Wextra -fexceptions -mthreads -DUNICODE -D_UNICODE -DWIN32 -DMINGW_HAS_SECURE_API=1 -DQT_QML_DEBUG -DQT_CORE_LIB -I. -Iobjectbox\include -I..\..\..\Qt\5.12.11\mingw73_64\include -I..\..\..\Qt\5.12.11\mingw73_64\include\QtCore -Idebug -I..\..\..\Qt\5.12.11\mingw73_64\mkspecs\win32-g++ -o debug\main.o main.cpp
In file included from main.cpp:2:0:
objectbox\include/objectbox.hpp:711:21: warning: 'std::vector<long long unsigned int> obx::{anonymous}::idVectorOrThrow(OBX_id_array*)' defined but not used [-Wunused-function]
std::vector<obx_id> idVectorOrThrow(OBX_id_array* cIds) {
^~~~~~~~~~~~~~~
objectbox\include/objectbox.hpp:703:14: warning: 'OBX_id_array obx::{anonymous}::cIdArrayRef(const std::vector<long long unsigned int>&)' defined but not used [-Wunused-function]
OBX_id_array cIdArrayRef(const std::vector<obx_id>& ids) {
^~~~~~~~~~~
g++ -Wl,-subsystem,console -mthreads -o debug\ObjectBoxTutorial.exe debug/main.o -LC:\temp\obxTut\ObjectBoxTutorial\objectbox\lib -lobjectbox C:\Qt\5.12.11\mingw73_64\lib\libQt5Cored.a
debug/main.o: In function `idVectorOrThrow':
C:\temp\obxTut\ObjectBoxTutorial/objectbox/include/objectbox.hpp:712: undefined reference to `obx::internal::throwLastError(int, char const*)'
C:\temp\obxTut\ObjectBoxTutorial/objectbox/include/objectbox.hpp:718: undefined reference to `obx::internal::throwIllegalStateException(char const*, char const*)'
debug/main.o: In function `applyTo':
C:\temp\obxTut\ObjectBoxTutorial/objectbox/include/objectbox.hpp:828: undefined reference to `obx::internal::throwIllegalStateException(char const*, char const*)'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [Makefile.Debug:65: debug/ObjectBoxTutorial.exe] Error 1
mingw32-make: *** [Makefile:38: debug] Error 2
mingw32-make[1]: Leaving directory 'C:/temp/obxTut/ObjectBoxTutorial'
23:05:08: The process "C:\Qt\Tools\mingw730_64\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project ObjectBoxTutorial (kit: Desktop Qt 5.12.11 MinGW 64-bit)
When executing step "Make"
Qt 5.12 with MinGW 7.3
The problem is the compiler that you are using, because the library is probably compiled with MSVC and you are trying to build your application with MinGW and use the C++ interface of the library.
this won't work because of name mangling and etc. see How to use libraries compiled with MingW in MSVC?
Thankfully this library also provides a pure C interface "objectbox.h", so including this header instead of the one with "hpp" would also fix your issue.
I was able to build and link a test app by using MSVC 2015 64 Bit.

Qt generated project code finds shared library file but still getting undefined references during build

I was given source code for a third party library for controlling a printer. I built a shared object from that. I was able to successfully use that shared object to run a simple program in C. I tried to integrate it with an existing Qt GUI that I built, but I was getting undefined reference errors. So, I made a new, clean Qt project, but I'm still getting the following errors:
/usr/bin/ld: main.o: in function `test_printer(int, char const**)':
/home/user/printer-test/debug/../printer-test/main.cpp:142: undefined reference to `sii_api_open_device(void**, char*)'
/usr/bin/ld: /home/user/printer-test/debug/../printer-test/main.cpp:143: undefined reference to `sii_api_close_device(void*)'
The .pro and main.cpp are:
printer-test.pro
QT += quick
CONFIG += c++17
HEADERS += sii_api.h
SOURCES += main.cpp
RESOURCES += qml.qrc
TRANSLATIONS += printer-test_en_US.ts
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
# Qt generated the following lines when given my library path
unix:!macx: LIBS += -L$$PWD/printer_lib/ -lsii
INCLUDEPATH += $$PWD/printer_lib
DEPENDPATH += $$PWD/printer_lib
# I added this one afterward
unix:!macx: LIBS += -lrt
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "sii_api.h" //< Library header
void test_printer(int argc, const char *argv[])
{
SIIAPIHANDLE hSiiApiHandle = nullptr;
sii_api_open_device( &hSiiApiHandle, const_cast<char *>(argv[2]) );
sii_api_close_device( hSiiApiHandle );
}
int main(int argc, char *argv[])
{
//<Qt setting up the gui and engine>
const char* args[] = {"", "0", "/dev/ttyUSB0"};
test_printer(3, args);
return app.exec();
}
The qmake generates a Makefile that looks something like this:
<snip>
LIBS = $(SUBLIBS) -L/home/user/printer-test/printer-test/printer_lib/ -lsii -lrt <snip>
printer-test: $(OBJECTS)
$(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)
<snip>
I was able to get the library linked with a sample C program given to me by the manufacturer. Here is the Makefile I used for the C program:
PROGRAM = sample
all::
gcc $(PROGRAM).c -o $(PROGRAM) -L ./printer_lib -lsii -lrt
clean::
rm -f $(PROGRAM) *.o
As seen in the sample Makefile, I needed to include the rt library as well. The Makefile did not work unless the rt library was included after the sii library. Still, my Qt program does not link.
Trying to compile the sample code as a C++ program instead of a C program gave me the clue I needed.
I needed to include extern "C" around the API header.
My Qt program compiles now.

Rosbag reader in qtcreator

I try to make a rosbag reader in Qtcreator. I create a project and I just want to read informations inside the bag.
Here is my pro file:
QT += core
QT -= gui
CONFIG += c++11
TARGET = ROSBag_Reader
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
LIBS += -lboost_system
LIBS+= -lpthread
LIBS+= -L/usr/lib/x86_64-linux-gnu
LIBS+= -ltf2_ros
LIBS+= -lrostime
LIBS+= -lrospack
LIBS+= -lroslz4
LIBS+= -lroslib
LIBS+= -lroscpp_serialization
LIBS+= -lroscpp
LIBS+= -lrosconsole_print
LIBS+= -lrosconsole_bridge
LIBS+= -lrosconsole_backend_interface
LIBS+= -lrosconsole
LIBS+= -lrosbag_storage
LIBS+= -lrosbag
SOURCES += main.cpp
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
And my main.cpp:
#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
int main(int argc, char *argv[])
{
rosbag::Bag bag;
bag.open("/home/xxx/Bureau/Developpement/ROSBag_Reader/data/dataset_table.bag", rosbag::bagmode::Read);
std::vector<std::string> topics;
topics.push_back(std::string("chatter"));
topics.push_back(std::string("numbers"));
rosbag::View view(bag, rosbag::TopicQuery(topics));
foreach(rosbag::MessageInstance const m, view)
{
std_msgs::String::ConstPtr s = m.instantiate<std_msgs::String>();
// if (s != NULL)
// std::cout << s->data << std::endl;
/*
std_msgs::Int32::ConstPtr i = m.instantiate<std_msgs::Int32>();
if (i != NULL)
std::cout << i->data << std::endl;*/
}
bag.close();
}
But when I try to run the project I get that error:
/home/xxx/Qt/5.7/gcc_64/bin/qmake -spec linux-g++ -o Makefile ../ROSBag_Reader/ROSBag_Reader.pro
g++ -Wl,-O1 -Wl,-rpath,/home/xxx/Qt/5.7/gcc_64/lib -o ROSBag_Reader main.o -lboost_system -L/usr/lib/x86_64-linux-gnu -ltf2_ros -lrostime -lrospack -lroslz4 -lroslib -lroscpp_serialization -lroscpp -lrosconsole_print -lrosconsole_bridge -lrosconsole_backend_interface -lrosconsole -lrosbag_storage -lrosbag -L/home/xxx/Qt/5.7/gcc_64/lib -lQt5Core -lpthread
/usr/bin/ld: main.o: référence au symbole non défini «_ZN3ros6HeaderC1Ev»
//usr/lib/x86_64-linux-gnu/libcpp_common.so.0d: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Makefile:214 : la recette pour la cible « ROSBag_Reader » a échouée
I don't understand where this error comes.
Did you try what the error messages suggests you to do? add -lcpp_common to your LIBS

Undefined reference error while compiling Qt hello world program

I am trying to compile a Qt hello world program. I am using Qt 5.2.0 on Windows 64 bit.
This is my code
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "Hello world";
return a.exec();
}
When i run the application i get the following run time errors:
mingw32-make[1]: Entering directory 'C:/Users/test/qt_creator_projects/build-QtTest- TestLocal_PC-Debug'
g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_CORE_LIB -I..\QtTest -
I"D:\Qt\5.2.0\msvc2012_64\include" -I"D:\Qt\5.2.0\msvc2012_64\include\QtCore" -I"debug" -
I"." -I"D:\Qt\5.2.0\msvc2012_64\mkspecs\win32-g++" -o debug\main.o ..\QtTest\main.cpp
g++ -Wl,-subsystem,console -mthreads -o debug\QtTest.exe debug/main.o -
LD:\Qt\5.2.0\msvc2012_64\lib -lQt5Cored
debug/main.o: In function `main':
C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC-
Debug/../QtTest/main.cpp:5: undefined reference to `_imp___ZN16QCoreApplicationC1ERiPPci'
C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC- Debug/../QtTest/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplication4execEv'
Makefile.Debug:77: recipe for target 'debug\QtTest.exe' failed
C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC-
Debug/../QtTest/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
mingw32-make[1]: Leaving directory 'C:/Users/test/qt_creator_projects/build-QtTest- TestLocal_PC-Debug'
Makefile:34: recipe for target 'debug' failed
C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC-Debug/../QtTest/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [debug\QtTest.exe] Error 1
mingw32-make: *** [debug] Error 2
16:37:34: The process "D:\pen drive data\MinGW\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project QtTest (kit: TestLocal PC)
When executing step 'Make'
.pro file
QT += core
QT -= gui
TARGET = QtTest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
I want to use Qt with MinGW .I am currently using Qt creatore for 64 bit Visual Studio 2012 compiler. Since 64 bit MinGW is not available in the downloads for 5.2.0
Can someone help me solve these reference errors?
Why would you download a VS2012 version of Qt if you don't have, well, VS2012?
Download a version that matches the exact compiler you have. If there is none, download the sources and compile it yourself.
If you want to avoid the complications of self-compilation, then VS2012 Express is perfectly adequate to develop with Qt. It's the same compiler as the paid-for versions, and if you develop using Qt Creator, then you really don't care for whatever limitations the express edition IDE might have. It works great.
Another possibility is to use the mingw build of Qt. During setup, you can enable the installation of the MinGW 4.8.0 toolchain. This gives you a complete development environment, with the IDE, the Qt headers and binaries, and a toolchain.

Inclusion of header files from include/Qt is deprecated

I've recently began working through a C++ Gui Programming with Qt 4 book.
However, I can't get past the first tutorials.
#include <QApplication>
#include <QTextEdit>
int main(int argv, char **args)
{
QApplication app(argv, args);
QTextEdit textEdit;
textEdit.show();
return app.exec();
}
Each time I try to compile this, I get this:
C:\Qt\4.8.0\andrew>qmake -project
C:\Qt\4.8.0\andrew>qmake
C:\Qt\4.8.0\andrew>make
mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Qt/4.8.0/andrew'
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -
DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -
DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\includ
e\QtCore" -I"..\include\QtGui" -I"..\include" -I"." -I"..\include\ActiveQt" -I"d
ebug" -I"..\mkspecs\win32-g++" -o debug\tutone.o tutone.cpp
In file included from tutone.cpp:1:0:
C:\Qt\4.8.0\include\Qt\Qapplication.h:3:10: warning: #warning "Inclusion of head
er files from include/Qt is deprecated." [-Wcpp]
In file included from tutone.cpp:2:0:
C:\Qt\4.8.0\include\Qt\Qpushbutton.h:3:10: warning: #warning "Inclusion of heade
r files from include/Qt is deprecated." [-Wcpp]
tutone.cpp: In function 'int qMain(int, char**)':
tutone.cpp:11:7: error: 'class QApplication' has no member named 'setMainWidget'
mingw32-make[1]: *** [debug/tutone.o] Error 1
mingw32-make[1]: Leaving directory `C:/Qt/4.8.0/andrew'
mingw32-make: *** [debug] Error 2
As I'm new to Qt and fairly new to compiling, I assume the error is on my end. Am I declaring the headers wrong or something?
My .pro file looks like this:
######################################################################
# Automatically generated by qmake (2.01a) Thu Jan 19 12:41:21 2012
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += tutone.h \
C:/Qt/4.8.0/include/Qt/Qapplication.h \
../include/QtGui/qapplication.h \
../src/gui/kernel/qapplication.h \
C:/Qt/4.8.0/include/Qt/Qpushbutton.h \
../include/QtGui/qpushbutton.h \
../src/gui/widgets/qpushbutton.h
SOURCES += tutone.cpp \
tutthree.cpp \
../src/gui/kernel/qapplication.cpp \
../src/gui/widgets/qpushbutton.cpp
Thanks
It's a false error due to a bug in the Qt build setup.
It's not a problem and has been logged on the Qt bug tracker
After running qmake -project you should add QT variable in the .pro file. Then you can remove these odd HEADERS and SOURCES (and link dynamically Qt libraries to your program):
QT += core gui # <-- this line
TEMPLATE = app
TARGET = ProjectNameHere
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += tutone.h
SOURCES += tutone.cpp \
tutthree.cpp \