cannot get makefile to work with Qt library - qt

I have 3 files in my program: App_interface.h, App_interface.cpp, main.cpp. Im trying to compile my program which requires the Qt library. I just installed the Qt library in the default location. This is my makefile:
if your wondering why i don't use moc in this makefile is because 1) i dont know how to do that. 2) i just want to stop getting the compiler error "cannot find ... file".
thanks
# Project: App_interface
# 10-19-09
#
# general variables
CPP := g++
OBJS := main.o App_interface.o
# Qt directorys
QTLIB := /usr/local/Trolltech/Qt-4.5.3/lib
QTINC := /usr/local/Trolltech/Qt-4.5.3/include
QTMOC := /usr/local/Trolltech/Qt-4.5.3/bin
App_interfaceV1: $(OBJS)
$(CPP) $(OBJS) -o App_interfaceV1 $(QTLIB)
main.o: main.cpp App_interface.h
$(CPP) -B $(QTINC) -c main.cpp -o main.o
App_interface.o: App_interface.h
$(CPP) -B $(QTINC) -c App_interface.cpp -o App_interface.o

There are several problems: You should use -I $(QTINC) to compile files, -L $(QTLIB) and at least -lQtCore -lQtGui to link the application. To get more help you should provide exact error messages.
Sorry to be blunt, but if you don't know how to write Makefiles, I'd suggest to use qmake.

Related

gcc Compling and linking object file and source file at the same time

In a nutshell , the issue is I have some object file (say a.o , b.o ) and some source file (f.c and g.c)
I have to compile and link in a single step.
This is what I am doing but I don't think this is the write way atleast I should give something like -l or -L for linking
gcc -Wall -O0 -ggdb -fPIC a.o b.o f.c g.c -o executable
This is the correct way, -l is used to link to a library (for example, the ptheads library needs -lpthread) and -L is used to add directories to the directories in which the linker looks for library files.
There is nothing wrong with compiling the software this way.
Most build systems build files one at a time in order to avoid having to rebuild the object file in case the source file was not modified and the object file still exists. If you are fine with rebuilding code, then your approach is perfectly valid.

Can't link against libsqlite3.a to create my own static library

I'm creating a static library that contains some objects including one that depends on sqlite3. This is my make file; it basically compiled the 3 cpp files into objects, put them into a .a lib and then use this library to get my main binary
CPPSRCS := \
$(LIBDIR)/database/active_database.$(SRCEXT) \
$(LIBDIR)/sd_card/sd_card.$(SRCEXT) \
$(LIBDIR)/led/led.$(SRCEXT)
OBJS := $(CPPSRCS:.cpp=.o)
lib/%.o: %.$(SRCEXT)
$(CXX) $(CXXFLAGS) -c $^ -o $# -lsqlite3
lib_$(APP).a: $(OBJS)
$(AR) rcs lib_$(APP).a $(OBJS)
$(APP): lib_$(APP).a
$(CXX) $(CXXFLAGS) $(SRCDIR)/$#.$(SRCEXT) -o $(DESTDIR)/$# lib_$(APP).a
My static library is built but when trying to get my binary against it I get undefined reference:
lib_XX.a(active_database.o): In function `database::ActiveDatabase::sqlite_exec_wrapper(std::string, int (*)(void*, int, char**, char**), void*)':
database/active_database.cpp:174: undefined reference to `sqlite3_exec'
database/active_database.cpp:178: undefined reference to `sqlite3_free'
lib_XX.a(active_database.o): In function `database::ActiveDatabase::init_db()':
I can see that -lsqlite3 (in /usr/lib) is not included in the compilation but I can't seem to force it to be included when I build my static library.
-lsqlite3 is ignored when compiling into object files because no linking happens.
You have three options.
You can add the SQLite object files to your static library lib_XX.a.
You can perform a relocatable link and produce a single .o file which contains the entire static library and SQLite, using ld -r.
Or consumers of your library need to link to your library and SQLite, using -l_XX -lsqlite3.
The last option is the most common approach and can be automated somewhat with tools like pkg-config.

How to create a Qt Creator project (.pro) file equivalent to existing Makefile

There is a project which is build via make command, where the Makefile is provided. Following are the Makefile contents:
TARGETT=gnulinux
OROPATH=/usr/local
CXXFLAGS=-I${OROPATH}/include -DOROCOS_TARGET=${TARGETT} -Wall
LDFLAGS=-L${OROPATH}/lib -Wl,-rpath ${OROPATH}/lib -lorocos-taskbrowser-${TARGETT} -lorocos-rtt-${TARGETT}
all: main
main.o: main.cpp
$(CXX) -c main.cpp $(CXXFLAGS)
AHRS.o: AHRS.cpp AHRS.h
$(CXX) -c AHRS.cpp $(CXXFLAGS)
main: main.o AHRS.o
$(CXX) -g -o main main.o AHRS.o $(CXXFLAGS) $(LDFLAGS)
clean:
rm -f main orocos.log .tb_history *.o
But I'm used to use QtCreator. I know that QtCreator constructs its own makefile from the project (.pro) file during build process. So, I think, relevant information must be copied from the Makefile contents (above) into a Qt Creator project file to allow QtCreator to generate the compatible makefile. Right?
Given the Makefile above, how should the .pro file look like?
And in the end of this process I'll be able to work from QtCreator.
Given the Makefile above, how should the .pro file look like?
Like this:
TEMPLATE = app
TARGET = AHRS # the desired filename of the executable goes here
OROPATH = /usr/local
INCLUDEPATH += $${OROPATH}/include
O_TARGET = gnulinux
DEFINES += OROCOS_TARGET=$${O_TARGET}
QMAKE_RPATHDIR += -L$${OROPATH}/lib
LIBS += -L$${OROPATH}/lib
LIBS += -lorocos-taskbrowser-$${O_TARGET} -lorocos-rtt-$${O_TARGET}
SOURCES += main.cpp AHRS.cpp
HEADERS += AHRS.h
Note that Qt Creator doesn't construct anything. qmake does. The project files are qmake projects, not Qt Creator projects. You don't need Qt Creator at all to build your project. You can do it from the command line using nothing but Qt and the compiler/binutils:
# assume that the source is in AHRS-source subfolder
mkdir AHRS
cd AHRS
/path/to/Qt/bin/qmake ../AHRS-source
make -j
# now we can run it
./AHRS

Error linking static boost_filesystem library with Qt Creator

I am getting an error when trying to link a static boost_filesystem library with QMake.
I know that my boost install is working, because I can link to it no problem outside of QMake like this:
g++ -o my_file my_file.cpp -IC:\boost_1_55_0\boost_1_55_0 -LC:\boost_1_55_0\boost_1_55_0\stage\lib -lboost_system -lboost_filesystem
I tried linking Qt with boost_filesystem like this at first, but it seems that it could not find the libraries:
LIBS += -LC:\boost_1_55_0\boost_1_55_0\stage\lib -lboost_system -lboost_filesystem
"-lboost_system not found -lboost_filesystem not found"
I read something about how you need to specify absolute paths to static libs when using QMake, so I've changes my LIBS line in my .pro to the following:
LIBS += "C:\boost_1_55_0\boost_1_55_0\stage\lib\libboost_system-mgw48-mt-1_55.a" \
"C:\boost_1_55_0\boost_1_55_0\stage\lib\libboost_filesystem-mgw48-mt-1_55.a"
The libraries are found with this method, however I get the following error message:
undefined reference to boost::system::generic_category()
I was under the impression that this error only happen when boost_system isn't linked! I seems like it is linked though, because it finds the proper library for it, and I know my install of boost is good because the same library links fine (with the same compiler) outside of Qt Creator.
Here's the include in mainwindow.h:
include <boost/filesystem.hpp>
Here the full .pro:
QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
QMAKE_CXXFLAGS += -std=c++11
TARGET = Downloader
TEMPLATE = app
INCLUDEPATH += C:\boost_1_55_0\boost_1_55_0
LIBS += "C:\boost_1_55_0\boost_1_55_0\stage\lib\libboost_system-mgw48-mt-1_55.a" \
"C:\boost_1_55_0\boost_1_55_0\stage\lib\libboost_filesystem-mgw48-mt-1_55.a"
SOURCES += main.cpp\
mainwindow.cpp\
rar_process.cpp\
HEADERS += mainwindow.h\
rar_process.h\
FORMS += mainwindow.ui
RESOURCES += \
graphics.qrc
And here's the full compiler output:
C:\Qt\Qt5.1.1\5.1.1\mingw48_32\bin\qmake.exe -spec win32-g++ -o Makefile ..\qt- downloader\Downloader.pro
C:/Qt/Qt5.1.1/Tools/mingw48_32/bin/mingw32-make -f Makefile.Release
mingw32-make[1]: Entering directory 'C:/Users/Patrick/Desktop/cpp/build-Downloader- Desktop_Qt_5_1_1_MinGW_32bit-Release'
g++ -Wl,-s -Wl,-subsystem,windows -mthreads -o release\Downloader.exe release/main.o release/mainwindow.o release/rar_process.o release/qrc_graphics.o release/moc_mainwindow.o release/moc_rar_process.o -lglu32 -lopengl32 -lgdi32 -luser32 -lmingw32 -lqtmain C:\boost_1_55_0\boost_1_55_0\stage\lib\libboost_system-mgw48-mt-1_55.a C:\boost_1_55_0\boost_1_55_0\stage\lib\libboost_filesystem-mgw48-mt-1_55.a - LC:\Qt\Qt5.1.1\5.1.1\mingw48_32\lib -lQt5Widgets -lQt5Network -lQt5Gui -lQt5Core
release/main.o:main.cpp:(.text.startup+0x1a): undefined reference to `boost::system::generic_category()'
release/main.o:main.cpp:(.text.startup+0x24): undefined reference to `boost::system::generic_category()'
Makefile.Release:86: recipe for target 'release\Downloader.exe' failed
c:/qt/qt5.1.1/tools/mingw48_32/bin/../lib/gcc/i686-w64-mingw32/4.8.0/../../../../i686- w64-mingw32/bin/ld.exe: release/main.o: bad reloc address 0x24 in section `.text.startup'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [release\Downloader.exe] Error 1
mingw32-make[1]: Leaving directory 'C:/Users/Patrick/Desktop/cpp/build-Downloader- Desktop_Qt_5_1_1_MinGW_32bit-Release'
makefile:34: recipe for target 'release' failed
GCC is very sensitive to the order in which you specify libraries during the linking stage. For example, if libA.a depends on libB.a and an executable App depends on both, then you'd have to invoke linker in the following way:
gcc main.o object1.o ... object2.o -lA -lB -o App
This implies that you have to change it to:
LIBS += "C:\boost_1_55_0\boost_1_55_0\stage\lib\libboost_filesystem-mgw48-mt-1_55.a" \
"C:\boost_1_55_0\boost_1_55_0\stage\lib\libboost_system-mgw48-mt-1_55.a"
To be cross-platform, it's recommended to make your build more robust:
Boost_VERSION = 1_55
COMPILER = mgw48
win32-g++* {
LIBS += $$(Boost_DIR)/stage/lib/libboost_filesystem-$${COMPILER}-mt-$${Boost_VERSION}.a \
$$(Boost_DIR)/stage/lib/libboost_system-$${COMPILER}-mt-$${Boost_VERSION}.a
PRE_TARGETDEPS += $$(Boost_DIR)/stage/lib/libboost_filesystem-$${COMPILER}-mt-$${Boost_VERSION}.a \
$$(Boost_DIR)/stage/lib/libboost_system-$${COMPILER}-mt-$${Boost_VERSION}.a
}
linux-g++* {
LIBS += -L$$(Boost_DIR)/stage/lib -lboost_filesystem-$${COMPILER}-mt-$${Boost_VERSION} \
-lboost_system-$${COMPILER}-mt-$${Boost_VERSION}
PRE_TARGETDEPS += $$(Boost_DIR)/stage/lib/libboost_filesystem-$${COMPILER}-mt-$${Boost_VERSION}.a \
$$(Boost_DIR)/stage/lib/libboost_system-$${COMPILER}-mt-$${Boost_VERSION}.a
}
NOTE: Boost_DIR would be treated as an environment variable here. So that you can flexibly build it on any system without hardcoding the system-dependent path to Boost. This is general practice in professional software development: try to hardcode as little as possible in your build system, whatever it is. Furthermore, it's reasonable to even make Boost_VERSION environment variable too. All you'd have to change then, are brackets to parentheses, i.e. $${Boost_VERSION} to $$(Boost_VERSION).
Finally, notice that I've used forward slash / as a path separator for both Windows and Linux. Backward slash \ is not only deprecated in QMake, but it is also discouraged in general. Once again, be cross-platform: simply let QMake handle the conversion.
Apparently boost-system will not link against mingw gcc 4.8.0 if it was built with mingw gcc 4.8.1.
I re-built boost with mingw gcc 4.8.0 and linked against those libs, and now it works!

How can I get make to put the binaries in a different location?

Short and easy question, but I seem to have a writer's block here:
Suppose I have a source code file in the same directory as the makefile I use to build the program:
confus#confusion:~/prog$ ls
binaries includes main.c Makefile
How do I get make to put the binaries for my main.c in the binaries dir? Afterwards on a second run make should see if the binary file there is up to date (and don't compile it again) just like normal.
My thought was something like this:
# Makefile
.PHONY: all
SOURCES := $(wildcard *.c)
TARGETS := $(subst %.c,binaries/%.o,$(SOURCES))
all:$(TARGETS)
$(TARGETS):$(SOURCES)
./compile "$(subst .o,.c,$(#F))" -o "$#"
Don't say all targets depend on all sources, instead have a pattern rule
binaries/%.o: %.c
./compile ... -o $# -c $<
you may also need to use a vpath
Revised:
You also had a problem with your subst ...
this test worked (just for compiling individual .o files, you still need to link them, which would be a very simple rule)
# Makefile
.PHONY: all
SOURCES := $(wildcard *.c)
TARGETS := $(patsubst %.c,binaries/%.o,$(SOURCES))
all:$(TARGETS)
binaries/%.o: %.c
$(CC) -o $# -c $<

Resources