How to build Qt project in linux with cmake - c++

I am using ubuntu 14.04, cmake 2.8.12.2, Qt5.6.2 (a built version), GNU make 3.81
After I run cmake with cmake PathToSource -G "Eclipse CDT4 - Unix Makefiles"
I do make. I get #error "You must build your code with position independent code if Qt was built with -reduce-relocations. " "Compile your code with -fPIC (-fPIE is not enough)."
# error "You must build your code with position independent code if Qt was built with -reduce-relocations. "\
I then download source file of Qt5.7.0, build and install it without problem. I do again cmake PathToSource -G "Eclipse CDT4 - Unix Makefiles", make it. I get many errors, such as /home/sflee/Documents/Software_dev/3rd_party/Qt5.7.0/include/QtCore/qhash.h:957:10: error: ‘pair’ does not name a type
auto pair = qAsConst(*this).equal_range(akey); and /home/sflee/Documents/Software_dev/3rd_party/Qt5.7.0/include/QtCore/qbasicatomic.h:285:14: error: ‘Ops’ has not been declared
{ return Ops::fetchAndAddRelease(_q_value, valueToAdd); }
How to solve it?

Qt 5.7 requires C++11 compiler. If you get that kind of error from auto pair, it sounds like your compiler is not compiling C++11 code. There are two possible reasons:
You just need to pass -std=c++11 to your compiler, as explaned under this question.
You have too old compiler. However, since you compiled Qt 5.7 itself with the same compiler, this shouldn't be the problem for you.

Related

How to use Clang kit with Qt Creator on Windows?

(this question similar to this one but is not the same)
I'm using QtCreator on Windows platform, usually with vc toolchain.
sometimes with MinGW-W64, so for now, I'd like to try clang for some reasons.
unlike the post above - I don't use Qt library , just qtcreator as IDE, so I suppose I don't need to re-build it and QtCreator for a using clang kit, is it correct?
there are a few questions about that:
CLang distribution doesn't contain platform or even c/c++ runtime library, should i use it from vc kit? or/and MinGW runtime? how to switch between?
Correct my understanding please if it's wrong - for Win platform, clang has two options to use: 1) normal use - clang.exe ,as on any other platform 2) clang-cl.exe - additional layer which "looks like" cl.exe, and just parses cl command line keys and calls normal clang.
as I understand there is no LLDB for Windows platform , can I use GDB or CDB depends of the used runtime lib and binary format of the executable?
and finally - how to configure all this in qtcreator?
The following steps apply only to MSYS2 64-bit installation of QT Creator 4.5.1 (install instructions here), where you also have MinGW-w64 and mingw32-make installed on MSYS2; and you are building a non-QT C or C++ application.
These instructions use QMake, because QBS doesn't support MSYS2 clang. Well, QMake doesn't support it either, but I did figure out how to add support to QMake and I didn't figure out QBS.
There is QMake support for MSVC-clang but it outputs MSVC makefiles, so you can't build it with MSYS2 make. So that does not apply to us.
Install clang with pacman. I used pacman -Ss mingw-w64-x86_64-clang, your flavour may vary.
Add support for clang to QMake:
In the MSYS2 shell, go into /msys64/mingw64/share/qt5/mkspecs/
Do cp -a win32-g++ win32-clang-msys
Edit win32-clang-msys/qmake.conf and change gcc to clang, and g++ to clang++ (2 places each)
In the same file, take out -fno-keep-inline-dllexport -mthreads which are not supported by clang.
In QT Creator, set up a new Kit:
Go to Manage Kits.
Add a Custom Compiler for C and browse to the installed path (/mingw64/bin/clang.exe under your MSYS2 install).
Add a Custom Compiler for C++ as clang++.exe in the same place)
Add a manual Kit called Clang and set those two compilers as its compilers.
In the manual kit config set "Qt mkspec" as win32-clang-msys
Set "QT Version" to something. Even though I am using a non-QT project, the IDE doesn't like using the kit if "Qt version" is set to None.
Now you can attempt to build your project with the Clang kit and QMake.
I initially tried with QBS and the build failed due to this bug . But the build commands do succeed if I copy-paste them and cut out the bogus -target switch. So for QBS users I guess you have to switch to QMake in the meantime until they fix that bug.
Troubleshooting: I sometimes got an error Project ERROR: failed to parse default search paths from compiler output. This is a problem with QMake's lack of support for clang. The error tended to not occur if I built in a subdirectory of the .pro file, but did occur if I built in a sibling directory.
As a workaround: go back into win32-clang-msys/qmake.conf. Change the first clang back to g++. Then "Run Qmake" (from QT creator or commandline), then change it back. The first time you run QMake it writes the file .qmake.stash and then does not need to generate it again. The contents of this file were bogus for me but building seemed to work anyway.
Undefined references: I found that linking with -static produced a bunch of undefined references to __imp__cxa_ names. Not sure what the problem is here but maybe related to the bug with generating .qmake.stash. I guess the Qt developers would need to officially add non-MSVC Clang support to QMake.
Multiple definitions: The CLang linker gave multiple definitions for inline DLLexport functions. I found no workaround yet for this; g++ has -fno-keep-inline-dllexport to avoid this problem but CLang 5 does not support that flag.

How to pass nonfragile-abi flag to cmake?

I would like to create an install script for objectiveC with arc and dispatch_queue support for Raspberry Pi. This script will be open sourced in github. Right now lots of the progress is working. But while installing libobjc2 the compiler complains:
error: -fobjc-arc is not supported with fragile abi
I know that I have to set the -fobjc-nonfragile-abi flag to the clang compiler.
Unfortunately I don't know lot about cmake and how to pass arguments to cmake.
This is suggested by GNUstep and works until the error appears:
cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
How do I tell cmake using the command line to use the "-fobjc-nonfragile-abi" flag?
You can always append defines like that with SET:
SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fobjc-nonfragile-abi")
But I haven't use clang, so It's only a guess one

Qt 4.8.2 With GCC 4.7.0.1 Keeps Crashing

I've downloaded Qt 4.8.2 library, Qt Creator 2.5.2, and manually installed MingW with w32api version 3.13 and GCC/g++ version 4.7.0.1. My OS is Windows 7 Ultimate x64.
I can create a sample "Plain C++ project" in Qt Creator; compile and run that console application using g++ without any issue.
But I cannot run a Qt application. I used Qt Creator, created a dummy Qt Application using Creator's "Qt Gui Application" template. The project can be compiled successfully, without any error or warning. But the binary keeps crashing when I try to run (both from Qt Creator and Windows Explorer) it. Both debug and release builds crash. It crashes even before showing main window.
MingW is installed in C:\MingW and C:\MingW\bin is in PATH. Qt is installed in C:\Qt\4.8.2 and C:\Qt\4.8.2\bin is in PATH.
I analyzed generated exe of the Qt Gui Application output with Dependency Walker and found that it found all required DLLs:
c:\windows\system32\KERNEL32.DLL
c:\windows\system32\MSVCRT.DLL
c:\mingw\bin\LIBGCC_S_DW2-1.DLL
c:\mingw\bin\LIBSTDC++-6.DLL
c:\qt\4.8.2\bin\QTCORE4.DLL
c:\qt\4.8.2\bin\QTGUI4.DLL
So, what's causing the runtime crash?
EDIT
I also tried Qt's example projects: 2dpainting and addressbook - both crashed when they were launched.
You should build Qt with the MinGW compiler you're using to build your application. GCC is generally less sensitive to binary compatibility issues than MSVC is, but Qt is a big, complex framework library. If anything would expose those kinds of issues, Qt would probably be on the short list.
Building Qt is pretty straightforward, but it takes a lot of time and there always seems to be two or three patches I need to make to get things to build successfully.
The last time I built Qt (4.7.3) with MinGW, I had to make the following patches - I'm not sure whether they will still apply to Qt 4.8:
make sure not to enable C++11 mode in the compiler - there are several macros with concatenated string literals that break under the new C++11 extended literal syntax
there is a problem with how some distributions of MinGW incorporate the Microsoft extensions to float.h - I had to sometimes had to add the line:
#include_next <float.h>
to the end of the MinGW-specific float.h so the generic GCC float.h would get processed properly. I had to do this for nuwen 4.7.0 lib/gcc/i686-pc-mingw32/4.7.0/include/float.h and TDM 4.6.1 32-bit distro lib/gcc/mingw32/4.6.1/include/float.h (the 64-bit distro of TDM didn't need this patch).
patch qmake\Makefile.win32-g++ and qmake\Makefile.win32-g++-sh to remove the -static-libstdc++ option that GCC doesn't recognize (and now errors out on instead of ignores)
patch mkspecs/win32-g++/qmake.conf to move the -Wl, in the QMAKE_LFLAGS_EXCEPTIONS_ON macro to its proper place in QMAKE_FLAGS:
QMAKE_LFLAGS = -Wl,-enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc
QMAKE_LFLAGS_EXCEPTIONS_ON = -mthreads
copy make.exe to mingw32-make.exe in MinGW's bin directory if there's not already a mingw32-make.exe
Then building Qt consists of:
set QTDIR=<location of Qt source directory> # where configure.exe is
set PATH=%QTDIR%\bin;c:\MinGW\bin;%PATH%
set INCLUDE=
set LIB=
cd %QTDIR%
mingw32-make confclean # (this should fail the first time, since there's nothing to clean)
configure.exe -opensource -debug-and-release -nomake examples -nomake demos -nomake tests -platform win32-g++ # and accept the GPL license
mingw32-make
This takes a while... hopefully nothing else will need patching.
I also got this problem. I'm a Qt n00b and tought, when installing Qt-libraries, that "well I already have Mingw installed so I skip installing the Mingw that comes with Qt". That gave me prolems. When installing mingw that came with Qt everything worked ok.
So my advice to anyone googling to this question (like I did) is to instead of using your already installed Mingw, install the one with Qt and use that (otherwise you have to build the Qt libraries within your Mingw, like the answer from Michael Burr)

Setting up ccache with Qt Creator on Windows

I'm trying to set up ccache in Qt Creator on Windows.
Unfortunatelly I couldn't find any good instruction. How to make it working?
I have ccache installed with Cygwin.
edit:
I've tried adding
QMAKE_CXX+="ccache g++"
as additional parameter in qmake build step.
But in compile output I still have "g++ -c -g -Wall ..." and I suppose it should be "ccache g++ ..."
It should be:
"QMAKE_CXX=ccache g++"
And you might need to rerun qmake from the Build menu if QtCreator doesn't update automatically the makefile after you add that parameter.
QtCreator doesn't know about ccache, and besides, it's looking for trouble to mix Cygwin binaries with native binaries on Windows.

Trouble Building Trilinos (Scientific Computation Library) on Mac OS X

I am trying to build the Trilinos library on Mac OS X 10.6.8, basically following the instructions on this page and the CMake Quickstart document included with Trilinos.
In order to build it with CMake, the instructions recommend setting up a directory, e.g. /usr/local/trilinos, creating a ./do-configure script in it and running it. My script is:
EXTRA_ARGS=$#
cmake \
-D CMAKE_BUILD_TYPE:STRING=DEBUG \
-D Trilinos_ENABLE_TESTS:BOOL=ON \
$EXTRA_ARGS \
${TRILINOS_HOME}
Running this script starts the configuration, until an error appears. The verbose output can be found here: http://pastebin.com/abrEghD7
I think the relevant part of the message is:
-- Verifying Fortran/CXX Compiler Compatibility - Failed
CMake Error at /usr/local/Cellar/cmake/2.8.7/share/cmake/Modules/FortranCInterface.cmake:284 (message):
The Fortran compiler:
/usr/local/bin/gfortran
and the CXX compiler:
/usr/bin/c++
failed to compile a simple test project using both languages. The output
was:
However, I cannot explain why the script blames the Fortran and C++ compilers. I have tested the Fortran compiler with "Hello World", and used the C++ compiler of OS X for entire projects. As far as I can tell, both compilers work properly.
Can anyone guess what might be wrong?
EDIT: Seems like it is some compatibility issue, not a problem with the indivual compilers.
When I compile paraview. I set my CXX compiler to g++ and not the standard c++ as cmake does. Because otherwise I see a similar error.
Where did you get gfortran from? Builds from most sources don't support mac specific extensions like -arch which might make problems trying to combine both of them. Or they might just be different versions (if you're gfortran is much newer than your g++ they might be trying to pull in incompatible runtime versions).
In the numerical python community (which depends a lot on fortran) the recommended source for OS X is the binary from here. This is matched to the XCode gcc version and supports the Mac specific extensions.