Getting the output of a command in a Bakefile - c++

I'm just getting started on a project with wxWidgets, and I'm trying to set up a Bakefile for cross-platform compilation. I need to pass the output of wx-config --libs and wx-config --cxxflags to the compiler.
How can I accomplish this? I cannot find anything in the Bakefile docs about getting the output of a command into a variable. Backticks don't seem to work:
myvar = `wx-config --libs`
#=> bakefile.bkl:2:12: error: no viable alternative at character u'`'

You need to use quotes here, i.e.
myvar = "`wx-config --libs`"
For the reference, here is what I do in my own bakefiles:
if ( $toolset == gnu || $toolset == gnu-osx ) {
setting WX_CONFIG {
default = wx-config;
help = "Path to the wx-config script";
}
compiler-options += "`$(WX_CONFIG) --cppflags`";
link-options += "`$(WX_CONFIG) --libs`";
}
This allows to do things like make WX_CONFIG=/full/path/to/wx-config which is handy when using an uninstalled version of wxWidgets.

Related

wxWidgets autoconf setup doesn't set include dirs as -Isystem but as -I

I have inherited a build system that uses autoconf and automake to build. I am not at all familiar with how this works except on a high level.
The build system itself works fine. However a lot of warnings are generated from system headers. In my case wxWidgets. The cause of this is that the include dir of wxWidgets is passed as -I instead of -isystem. I have searched online but I can't find how I can change this.
The following code is in the configure.ac file:
AM_OPTIONS_WXCONFIG
AM_PATH_WXCONFIG(3.0.2, wxWin=1, , ,[--debug=yes])
if test "$wxWin" != 1; then
AC_MSG_ERROR([
wxWidgets must be installed on your system
but no wx-config script could be found.
Please check that wx-config is in path, the directory
where wxWidgets libraries are installed as returned by
'wx-config --libs' is in the linker path (LD_LIBRARY_PATH
or equivalent variable) and wxWidgets version is 2.9.4 or
above.
])
fi
if test "$wxWin" != 0; then
AC_DEFINE(HAVE_WXWIDGETS,1,[define if the wxWidgets 3.0.2. or higher is available])
fi
Then there is a Makefile.am which does the following:
bin_PROGRAMS = project
project_SOURCES = {sources here}
project_CPPFLAGS = $(AM_CPPFLAGS) $(PROJECT_CPPFLAGS) $(WX_CPPFLAGS)
project_CFLAGS = $(AM_CFLAGS) $(PROJECT_CFLAGS) $(WX_CFLAGS)
project_CXXFLAGS = $(AM_CXXFLAGS) $(PROJECT_CXXFLAGS) $(WX_CXXFLAGS)
project_LDFLAGS = $(AM_LDFLAGS)
project_LDADD = $(AM_LIBADD) $(WX_LIBS)
There doesn't seem an option that I can specify it as a system header in some way.
Do anyone of you know how I can tackle this?
The problem has nothing to do with autoconf. It's the wx-config tool that is the culprit. That tool is used to obtain the include directories. For instance invoking wx-config --cxxflag will result in this:
-I/usr/lib/wx/include/gtk2-unicode-3.1 -I/usr/include/wx-3.1 -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__ -pthread
Which is passed to the compiler. There are a couple of ways of solving this. Either manually replace it in the output or patch wx-config.

How Do I Get WxWidgets setup.h after I install all the .debs on Debian?

Problem first, then details:
I copied a hello-world program from the wxwidgets tutorials and tried to compile it from the command line like this:
g++ -o h wxhello.cpp -I/usr/include/wx-3.0
The compile terminated quickly because it could not find "wx/setup.h". I researched this apparently EXTREMELY COMMON PROBLEM and learned that there is supposed to be a second include path, pointing to the place where the individual setup.h that suits my situation can be found. So I tried:
find /usr/include/wx-3.0 -name "setup.h"
And the output was nothing.
So I installed wxWidgets by marking libwxgtk3.0-dev in Synaptic and allowing all the dependencies to be installed (something like 40 packages in all because I just set this thing up).
How do I get my program to compile?
You need to have wx-config --cxxflags --libs in your command line enclosed in back-ticks like this
g++ -o h wxhello.cpp `wx-config --cxxflags --libs`
This is not the solution on Windows, but it should work on any Linux.
This is not documented in a searchable fashion as far as I can tell.

QMake not finding GraphicsMagick libraries

I am trying to link a Qt program with GraphicsMagick, but Qt/qmake refuses to search /usr/local/* for GraphicsMagick++-config. I am using the $$system() command to fetch the flags like so:
QMAKE_CXXFLAGS += $$system(GraphicsMagick++-config --cppflags --cxxflags)
QMAKE_LIBS += $$system(GraphicsMagick++-config --ldflags --libs)
But every time, qmake complains /bin/sh: GraphicsMagick++-config: command not found. I have tested my path, and I can see /usr/local/bin from sh, csh, zsh, and bash. I am not sure why Qt can't find this command. Is there something simple I am missing?
I am running OS X 10.9, with Qt 5.3.1.
Make sure that you run qmake from the same shell where you did the PATH test, running it through a Finder-launched QtCreator might not inherit the environment if you set it in your ~.*shrc file.
I tried your code and qmake properly calls GraphicsMagick++-config with my brew installed GraphicsMagick.
FWIW, even though you might get the same error, a better way to get it to work would be to let qmake figure out the pkgconfig logic itself:
CONFIG += link_pkgconfig
PKGCONFIG = GraphicsMagick++

Qt: Check gcc Version in pro-File [duplicate]

Is there any way to get the version and vendor of the compiler used by the user through qmake? What I need is to disable building some targets of my project when g++ 3.x is used and enable them when g++ 4.x is used.
Update: Most answers targeted the preprocessor. This is something that I want to avoid. I don't want a target to be build for a specific compiler version and I want this decision to be made by the build system.
In addition to ashcatch's answer, qmake allows you to query the command line and get the response back as a variable. So you could to something like this:
linux-g++ {
system( g++ --version | grep -e "\<4.[0-9]" ) {
message( "g++ version 4.x found" )
CONFIG += g++4
}
else system( g++ --version | grep -e "\<3.[0-9]" ) {
message( "g++ version 3.x found" )
CONFIG += g++3
}
else {
error( "Unknown system/compiler configuration" )
}
}
Then later, when you want to use it to specify targets, you can use the config scoping rules:
SOURCES += blah blah2 blah3
g++4: SOURCES += blah4 blah5
My answer based on Caleb Huitt - cjhuitt's. But his approach does not work for me.
*-g++ {
GCC_VERSION = $$system("g++ -dumpversion")
contains(GCC_VERSION, 6.[0-9]) {
message( "g++ version 6.x found" )
CONFIG += g++6
} else {
contains(GCC_VERSION, 5.[0-9]) {
message( "g++ version 5.x found" )
CONFIG += g++5
} else {
contains(GCC_VERSION, 4.[0-9]) {
message( "g++ version 4.x found" )
CONFIG += g++4
} else {
message( "Unknown GCC configuration" )
}
}
}
}
As you see you can get version from GCC and then compare it with regex expression.
The way how to use is the same:
SOURCES += blah blah2 blah3
g++4: SOURCES += blah4 blah5
As a start, I would look at the scoping feature that qmake supports:
Scopes and Conditions
But while I read about it, it seems that by default you can use general platform conditions like win32 or unix or you can use the name of the qmake spec like linux-g++. You could test the Visual Studio version like this (since the different Visual Studio versions use different qmake specs), but I don't think that you can test the gcc version like this (at least I don't know how).
My answer is based on dismine's answer
We can simply extract the major version number using $$str_member
gcc | clang {
COMPILER_VERSION = $$system($$QMAKE_CXX " -dumpversion")
COMPILER_MAJOR_VERSION = $$str_member($$COMPILER_VERSION)
greaterThan(COMPILER_MAJOR_VERSION, 3): message("gcc 4 or later")
greaterThan(COMPILER_MAJOR_VERSION, 4): message("gcc 5 or later")
greaterThan(COMPILER_MAJOR_VERSION, 5): message("gcc 6 or later")
greaterThan(COMPILER_MAJOR_VERSION, 6): message("gcc 7 or later")
}
I do
isEmpty(MGWVER) {
MGW_MAJ = $$system(echo | gcc -dM -E - | fgrep __GNUC__ | cut -d\" \" -f 3)
MGW_MIN = $$system(echo | gcc -dM -E - | fgrep __GNUC_MINOR__ | cut -d\" \" -f 3)
MGWVER =$$MGW_MAJ$$MGW_MIN
message(MGWVER $$MGWVER)
}
It returns "48".
I use it for linking proper boost libraries:
USER_BOOST_CFG=mgw$${MGWVER}-mt-s-$$(BOOST_VER)
message($$USER_BOOST_CFG)
LIBS *= -L$$(BOOST_ROOT)/lib
LIBS *= -L$$(BOOST_ROOT)/stage/lib
LIBS *= -lboost_system-$$USER_BOOST_CFG
LIBS *= -lboost_filesystem-$$USER_BOOST_CFG
LIBS *= -lboost_date_time-$$USER_BOOST_CFG
effectively giving:
-lboost_system-mgw48-mt-s-1_54
I am on mingw.
Another idea is to look at QMAKESPEC vaariable and parse from it, hint:
message(QMAKESPEC $$QMAKESPEC)
SPLITED=$$section(QMAKESPEC, "/", 0, -3)
message(SPLITED $$SPLITED)
Each compiler vendor use to define some specific symbols that identify the compiler and version. You could make the check using those symbols.
I know, for example, that _MSC_VER gives the version of Microsoft C++ Compiler.
What I also know is that Boost Libraries use this kind of feature selection and adaptation.
You can take a look to Boost Config headers, found in include folder, at path: boost/config/* , specially at select_compiler_config.hpp.
By using those compiler specific symbols, you can make feature selection at preprocessing phase of building the code.
The following macros are defined in my version of gcc and g++
#define __GNUC__ 4
#define __GNUC_MINOR__ 0
#define __GNUC_PATCHLEVEL__ 1
Additionaly the g++ defines:
#define __GNUG__ 4

Finding compiler vendor / version using qmake

Is there any way to get the version and vendor of the compiler used by the user through qmake? What I need is to disable building some targets of my project when g++ 3.x is used and enable them when g++ 4.x is used.
Update: Most answers targeted the preprocessor. This is something that I want to avoid. I don't want a target to be build for a specific compiler version and I want this decision to be made by the build system.
In addition to ashcatch's answer, qmake allows you to query the command line and get the response back as a variable. So you could to something like this:
linux-g++ {
system( g++ --version | grep -e "\<4.[0-9]" ) {
message( "g++ version 4.x found" )
CONFIG += g++4
}
else system( g++ --version | grep -e "\<3.[0-9]" ) {
message( "g++ version 3.x found" )
CONFIG += g++3
}
else {
error( "Unknown system/compiler configuration" )
}
}
Then later, when you want to use it to specify targets, you can use the config scoping rules:
SOURCES += blah blah2 blah3
g++4: SOURCES += blah4 blah5
My answer based on Caleb Huitt - cjhuitt's. But his approach does not work for me.
*-g++ {
GCC_VERSION = $$system("g++ -dumpversion")
contains(GCC_VERSION, 6.[0-9]) {
message( "g++ version 6.x found" )
CONFIG += g++6
} else {
contains(GCC_VERSION, 5.[0-9]) {
message( "g++ version 5.x found" )
CONFIG += g++5
} else {
contains(GCC_VERSION, 4.[0-9]) {
message( "g++ version 4.x found" )
CONFIG += g++4
} else {
message( "Unknown GCC configuration" )
}
}
}
}
As you see you can get version from GCC and then compare it with regex expression.
The way how to use is the same:
SOURCES += blah blah2 blah3
g++4: SOURCES += blah4 blah5
As a start, I would look at the scoping feature that qmake supports:
Scopes and Conditions
But while I read about it, it seems that by default you can use general platform conditions like win32 or unix or you can use the name of the qmake spec like linux-g++. You could test the Visual Studio version like this (since the different Visual Studio versions use different qmake specs), but I don't think that you can test the gcc version like this (at least I don't know how).
My answer is based on dismine's answer
We can simply extract the major version number using $$str_member
gcc | clang {
COMPILER_VERSION = $$system($$QMAKE_CXX " -dumpversion")
COMPILER_MAJOR_VERSION = $$str_member($$COMPILER_VERSION)
greaterThan(COMPILER_MAJOR_VERSION, 3): message("gcc 4 or later")
greaterThan(COMPILER_MAJOR_VERSION, 4): message("gcc 5 or later")
greaterThan(COMPILER_MAJOR_VERSION, 5): message("gcc 6 or later")
greaterThan(COMPILER_MAJOR_VERSION, 6): message("gcc 7 or later")
}
I do
isEmpty(MGWVER) {
MGW_MAJ = $$system(echo | gcc -dM -E - | fgrep __GNUC__ | cut -d\" \" -f 3)
MGW_MIN = $$system(echo | gcc -dM -E - | fgrep __GNUC_MINOR__ | cut -d\" \" -f 3)
MGWVER =$$MGW_MAJ$$MGW_MIN
message(MGWVER $$MGWVER)
}
It returns "48".
I use it for linking proper boost libraries:
USER_BOOST_CFG=mgw$${MGWVER}-mt-s-$$(BOOST_VER)
message($$USER_BOOST_CFG)
LIBS *= -L$$(BOOST_ROOT)/lib
LIBS *= -L$$(BOOST_ROOT)/stage/lib
LIBS *= -lboost_system-$$USER_BOOST_CFG
LIBS *= -lboost_filesystem-$$USER_BOOST_CFG
LIBS *= -lboost_date_time-$$USER_BOOST_CFG
effectively giving:
-lboost_system-mgw48-mt-s-1_54
I am on mingw.
Another idea is to look at QMAKESPEC vaariable and parse from it, hint:
message(QMAKESPEC $$QMAKESPEC)
SPLITED=$$section(QMAKESPEC, "/", 0, -3)
message(SPLITED $$SPLITED)
Each compiler vendor use to define some specific symbols that identify the compiler and version. You could make the check using those symbols.
I know, for example, that _MSC_VER gives the version of Microsoft C++ Compiler.
What I also know is that Boost Libraries use this kind of feature selection and adaptation.
You can take a look to Boost Config headers, found in include folder, at path: boost/config/* , specially at select_compiler_config.hpp.
By using those compiler specific symbols, you can make feature selection at preprocessing phase of building the code.
The following macros are defined in my version of gcc and g++
#define __GNUC__ 4
#define __GNUC_MINOR__ 0
#define __GNUC_PATCHLEVEL__ 1
Additionaly the g++ defines:
#define __GNUG__ 4