Install C++ program with automake and autoconf - c++

I've made a program in C++, but now I must install this program with autoconf and automake.
So, when I run command "./configure && make && make install", it must do the following:
compile program
create folder my_program inside /opt (example: /opt/my_program/) and in this folder I must also have all static libraries and source files
There must be symbolic link in /usr/local/bin for my_program
Libraries must be in /usr/local/lib (DONE - Thanks to #Galik )
Header files of my_program must be in /usr/local/include (DONE)
I've wrote this configure.ac script:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([my_program], [0.1], [my_mail])
AC_CONFIG_SRCDIR([AbsAlgorithm.hpp])
AM_INIT_AUTOMAKE
# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
AC_CHECK_HEADERS([stdlib.h string.h sys/time.h unistd.h wchar.h wctype.h])
# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL
AC_C_INLINE
AC_TYPE_SIZE_T
# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_MKTIME
AC_CHECK_FUNCS([gettimeofday memset mkdir])
LIBS="-ldl"
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
and this Makefile.am script:
AUTOMAKE_OPTIONS = foreign
AM_CXXFLAGS=-Wall -std=gnu++11 -DVERSION=\"$(VERSION)\" -DPROG="\"$(PACKAGE)\""
bin_PROGRAMS = algatorc
noinst_LIBRARIES = libalgatorc.a
libalgatorc_a_SOURCES = Timer.cpp
include_HEADERS = Timer.hpp TestSetIterator.hpp TestCase.hpp ETestSet.hpp EParameter.hpp Entity.hpp ParameterSet.hpp AbsAlgorithm.hpp Log.hpp JSON.hpp
algatorc_SOURCES = ParameterSet.cpp TestCase.cpp EParameter.cpp ETestSet.cpp TestSetIterator.cpp Entity.cpp Timer.cpp main.cpp JSON.cpp JSONValue.cpp
Now, when I run "./configure && make && make install" I don't get new folder called my_program in /opt. But, I now, I do have header files in /usr/local/include. I don't have lib files in /usr/local/lib. There is just one folder for python. I would like to have folder called my_program and inside that folder I would like to have static libs.
I am using Ubuntu 12.04 x64
I would appreciate any help. Thanks

Autotools is not really designed to put things in specific locations. The idea is that programs go in the programs directory $(bindir), libraries in the libraries directory $(libdir) etc and that the person installing everything gets to decide where those locations are.
So you should really only care about installing things relative to wherever the person running the installer wants them to be.
They do this by adding arguments to the configure script like:
configure --prefix=/opt/myprog
That will typically install programs in /opt/myprog/bin and libraries in /opt/myprog/lib etc...
You can add to the places that things get installed by setting special dir variables. For example to put libraries in a sub-directory of $(libdir) (default /usr/local/lib) you caan do:
myprog_librarydir = $(libdir)/myprog
And its not uncommon to do the same for the header files:
myprog_includedir = $(prefix)/include/myprog
That defines some destination folders you can refer to instead of the defaults:
myprog_include_HEADERS = \
Timer.hpp \
TestSetIterator.hpp \
TestCase.hpp \
ETestSet.hpp \
EParameter.hpp \
Entity.hpp \
ParameterSet.hpp \
AbsAlgorithm.hpp \
Log.hpp \
JSON.hpp
Those will now get installed into $(prefix)/include/myprog.
Similarly with the corresponding library:
myprog_library_LIBRARIES = libmyprog.a
libmyprog_a_SOURCES = \
ParameterSet.cpp \
TestCase.cpp \
EParameter.cpp \
ETestSet.cpp \
TestSetIterator.cpp \
Entity.cpp \
Timer.cpp \
JSON.cpp \
JSONValue.cpp
So basically you create a destination (installation) directory using:
mynamedir = $(prefix)/path/... whatever
That allows you to set destinations other than bin_, lib_ and include_ etc...
So instead of saying lib_LIBRARIES you can say myname_LIBRARIES.
Hope that helps.

Related

local header files available in the project but make gives No such file or directory error

From entire day I am trying to install OverSim [http://www.oversim.org/wiki/OverSimInstall]
The make file looks like this:
all: checkmakefiles
cd src && $(MAKE)
clean: checkmakefiles
cd src && $(MAKE) clean
cleanall: checkmakefiles
cd src && $(MAKE) MODE=release clean
cd src && $(MAKE) MODE=debug clean
rm -f src/Makefile
makefiles:
cd src && opp_makemake -f --deep --make-so -o inet -O out $$NSC_VERSION_DEF
checkmakefiles:
#if [ ! -f src/Makefile ]; then \
echo; \
echo '======================================================================='; \
echo 'src/Makefile does not exist. Please use "make makefiles" to generate it!'; \
echo '======================================================================='; \
echo; \
exit 1; \
fi
doxy:
doxygen doxy.cfg
tcptut:
cd doc/src/tcp && $(MAKE)
I am using Omnet5.1.1 as omnet4.2.2 is not supported on Ubuntu16.04, my gcc version is 5.4.1.
Every time I try to build this make all, it gives header file not found error where as in actual the header files are present inside the project directory.
In file included from applications/ethernet/EtherAppCli.cc:21:0:
applications/ethernet/EtherAppCli.h:21:22: fatal error: INETDefs.h: No such file or directory
The includes are done like this:
#include "INETDefs.h" //available at src/linklayer/contract/
#include "MACAddress.h" //available at src/base/
project structure:
How could I resolve this build error?
This is a basic difference between newer OMNeT++ versions 5.x and the older OMNeT++ versions 3.x and 4.x.
As far as I remember Oversim, it was released for build with OMNeT 3.x and 4.2 as well as the older INET releases.
These old versions used parameters like --deep to search for include files, that's why the included files are just named and not entered with a complete path.
The newer INET and OMNeT releases use hierarchical path settings for include files. The complete paths have to be given for the compiler to access the included file.
So for INET version 3.x and OMNeT++ version 5.x, an include looks like: #include "inet/common/INETDefs.h"
Oversim does not include the complete paths for included headers, that's why you have errors when using Oversim with newer OMNeT releases.
The first option is to either use an older OMNeT version. Either install an older GCC in parallel on your system or set-up a virtual machine with an older Ubuntu if you like.
The second (and more complex) option is to adopt all include paths or define all necessary paths via the -I option of the compiler/linker.
Frankly, I'd suggest to use the older OMNeT++ 4.2.2 version...
The #include directive searches first of all inside the same directory as the file containing the directive and then in a preconfigured list of standard system directories.
If you don't want to move the header files to the same directory as EtherAppCli.cc, you will have to add the paths to these header files to this preconfigured list, usually with the compiler option
-Ipath/to/dir
I'm not sure if this is what is intended in the app your'e compiling but this is more or less what you can do.
Check to see if you missed anything in the installation guide.
Use OMNet 4.6 instead. Keep using inet-20111118. You should be able to build inet normally. Then build Oversim

Linking files through makefiles

I have two directories at the same level. I want to use the header file created in one directory in the source file of the other directory. I want to achieve the same without providing relative or absolute path to the header file.
The directory structure is:
main_dir/
dir1/
main.cpp
Makefile.am
dir2/
abc.h
abc.cpp
Makefile.am
main.cpp needs abc.h. What should be written in makefiles of both the directories in order to link them?
Edit
Makefile.am of dir2
noinst_LIBRARIES = libabc.a
AM_CPPFLAG = -I$(main_dir)/dir2 \
$(APP_CFLAGS)
libabc_a_SOURCES = abc.h \
abc.cpp
Makefile.am of dir1
noinst_LIBRARIES = libmain.a
AM_CPPFLAG = -I$(main_dir)/dir1 \
$(APP_CFLAGS)
libmain_a_SOURCES = main.cpp
libmain_a_LIBADD = libabc.a
I get the error: No rule to make target 'libabc.a', needed by 'libmain.a'. Stop.
You can use the -I option with gcc or clang. (Note this is not directly related to makefiles)
Example: If the current directory is main_dir, and you're currently running the command:
gcc -c -o dir1/main.o dir1/main.cpp
you would need to change it to something like:
gcc -Idir2 -c -o dir1/main.o dir1/main.cpp
You already have
AM_CPPFLAG = -I$(main_dir)/dir1 \
$(APP_CFLAGS)
Simply add -I$(main_dir)/dir2 into it:
AM_CPPFLAG = -I$(main_dir)/dir1 \
-I$(main_dir)/dir2 \
$(APP_CFLAGS)
So now you can #include <abc.h> in your code.

dbus - how to set include paths

On my system dbus headers are placed in /usr/include/dbus-1.0/dbus/ and dbus-arch-deps.h is other location (what seems to be strange): /usr/lib/x86_64-linux-gnu/dbus-1.0/include/dbus/dbus-arch-deps.h In my program I include #include<dbus-1.0/dbus/dbus.h>but in every header file which include others path looks like this: #include<dbus/xxx.h> I can copy dbus-arch-deps.h to /usr/include/dbus-1.0/dbus/ but how to fix paths in dbus headers ?
Your system likely has pkg-config installed.
g++ $(pkg-config --cflags dbus-1) main.c
Pkgconfig contains a database of linker/compiler/etc. flags that are required to use specific libraries. See man pkg-config for more info.
First of all you need to install and configure it properly.
You should try this command :
sudo apt-get -y install dbus libdbus-1-dev libdbus-glib-1-2 libdbus-glib-1-dev
Now, here is the Makefile that you should write for compiling :
all:
g++ dbus.cpp -I/usr/include/dbus-1.0 \
-I/usr/lib/x86_64-linux-gnu/dbus-1.0/include \
-I/usr/include/glib-2.0 \
-I/usr/lib/x86_64-linux-gnu/glib-2.0/include/ \
-ldbus-1 \
-ldbus-glib-1
Now, you may include files like dbus/dbus.h, dbus/dbus-glib.h, etc.
You don't need to copy files.
Simply add the path of where dbus is located to your include path when compiling using the I flag:
example:
g++ -Wall -I /usr/include/dbus-1.0/ -o main.o
By using the location of where dbus is located (in the standard location of /usr/include, you can reference the files like the following in your source code:
#include <dbus/xxx.h>
Similarly, if you have to link against dbus you'll have to append that path to the Libraries inclusion path like so:
g++ -Wall -I /usr/include/dbus-1.0/ -o main.o -L <dbus library path>
Where dbus library path is where the libraries ofdbus` live. To figure this out, consult the web, or search your system.
UPDATE:
To achieve that in Qt-Creator (which I've never used), perhaps the following can help:
How to add include path in Qt Creator?

How can I set rpath on gcc binaries during bootstrap?

I am trying to build gcc 4.7.2 using a custom prefix $PREFIX
I have built and installed all the prerequisites into my prefix location, and then successfully configured, built and installed gcc.
The problem that I now have is that $PREFIX is not in the library search path, and therefore the shared libraries cannot be found.
$PREFIX/bin $ ./g++ ~/main.cpp
$PREFIX/libexec/gcc/x86_64-suse-linux/4.7.2/cc1plus: \
error while loading shared libraries: \
libcloog-isl.so.1: \
cannot open shared object file: No such file or directory
What works, but isn't ideal
If I export LD_LIBRARY_PATH=$PREFIX/lib then it works, but I'm looking for something which works without having to set environment variables.
If I use patchelf to set the RPATH on all the gcc binaries then it also works; however this involves searching out all elf binaries and iterating over them calling patchelf, I would rather have something more permanent.
What I think would be ideal for my purposes
So I'm hoping there is a way to have -Wl,-rpath,$PREFIX/lib passed to make during the build process.
Since I know the paths won't need to be changed this seems like the most robust solution, and can be also be used for when we build the next gcc version.
Is configuring the build process to hard code the RPATH possible?
What I have tried, but doesn't work
Setting LDFLAGS_FOR_TARGET prior to calling configure:
All of these fail:
export LDFLAGS_FOR_TARGET="-L$PREFIX/lib -R$PREFIX/lib"
export LDFLAGS_FOR_TARGET="-L$PREFIX/lib"
export LDFLAGS_FOR_TARGET="-L$PREFIX/lib -Wl,-rpath,$PREFIX/lib"
Setting LDFLAGS prior to calling configure:
export LDFLAGS="-L$PREFIX/lib -Wl,-rpath,$PREFIX/lib"
In any event I worry that these will override any of the LDFLAGS gcc would have had, so I'm not sure these are a viable option even if they could be made to work?
My configure line
For completeness here is the line I pass to configure:
./configure \
--prefix=$PREFIX \
--build=x86_64-suse-linux \
--with-pkgversion='SIG build 12/10/2012' \
--disable-multilib \
--enable-cloog-backend=isl \
--with-mpc=$PREFIX \
--with-mpfr=$PREFIX \
--with-gmp=$PREFIX \
--with-cloog=$PREFIX \
--with-ppl=$PREFIX \
--with-gxx-include-dir=$PREFIX/include/c++/4.7.2
I've found that copying the source directories for gmp, mpfr, mpc, isl, cloog, etc. into the top level gcc source directory (or using symbolic links with the same name) works everywhere. This is in fact the preferred way.
You need to copy (or link) to those source directory names without the version numbers for this to work.
The compilers do not need LD_LIBRARY_PATH (although running applications built with the compilers will need an LD_LIBRARY_PATH to the $PREFIX/lib64 or something like that - but that's different)
Start in a source directory where you'll keep all your sources.
In this source directory you have your gcc directory either by unpacking a tarball or svn...
I use subversion.
Also in this top level directory you have, say, the following source tarballs:
gmp-5.1.0.tar.bz2
mpfr-3.1.1.tar.bz2
mpc-1.0.1.tar.gz
isl-0.11.1.tar.bz2
cloog-0.18.0.tar.gz
I just download these and update to the latest tarballs periodically.
In script form:
# Either:
svn checkout svn://gcc.gnu.org/svn/gcc/trunk gcc_work
# Or:
bunzip -c gcc-4.8.0.tar.bz2 | tar -xvf -
mv gcc-4.8.0 gcc_work
# Uncompress sources.. (This will produce version numbered directories).
bunzip -c gmp-5.1.0.tar.bz2 | tar -xvf -
bunzip -c mpfr-3.1.1.tar.bz2 | tar -xvf -
gunzip -c mpc-1.0.1.tar.gz | tar -xvf -
bunzip -c isl-0.11.1.tar.bz2 | tar -xvf -
gunzip -c cloog-0.18.0.tar.gz | tar -xvf -
# Link outside source directories into the top level gcc directory.
cd gcc_work
ln -s ../gmp-5.1.0 gmp
ln -s ../mpfr-3.1.1 mpfr
ln -s ../mpc-1.0.1 mpc
ln -s ../isl-0.11.1 isl
ln -s ../cloog-0.18.0 cloog
# Get out of the gcc working directory and create a build directory. I call mine obj_work.
# I configure the gcc binary and other outputs to be bin_work in the top level directory. Your choice. But I have this:
# home/ed/projects
# home/ed/projects/gcc_work
# home/ed/projects/obj_work
# home/ed/projects/bin_work
# home/ed/projects/gmp-5.1.0
# home/ed/projects/mpfr-3.1.1
# home/ed/projects/mpc-1.0.1
# home/ed/projects/isl-0.11.1
# home/ed/projects/cloog-0.18.0
mkdir obj_work
cd obj_work
../gcc_work/configure --prefix=../bin_work <other options>
# Your <other options> shouldn't need to involve anything about gmp, mpfr, mpc, isl, cloog.
# The gcc build system will find the directories you linked,
# then configure and compile the needed libraries with the necessary flags and such.
# Good luck.
I've been using this configure option with gcc-4.8.0, on FreeBSD, after building and installing gmp, isl and cloog:
LD_LIBRARY_PATH=/path/to/isl/lib ./configure (lots of other options) \
--with-stage1-ldflags="-rpath /path/to/isl/lib -rpath /path/to/cloog/lib -rpath /path/to/gmp/lib"
and the resulting gcc binary does not need any LD_LIBRARY_PATH. The LD_LIBRARY_PATH for configure is needed because it compiles a test program to check for the ISL version, which would fail if it didn't find the ISL shared lib.
I tried it on Linux (Ubuntu) where it failed during configuring because the -rpath args were passed to gcc instead of ld. I could fix this by using
--with-stage1-ldflags="-Wl,-rpath,/path/to/isl/lib,-rpath,/path/to/cloog/lib,-rpath,/path/to/gmp/lib"
instead.
Just using configure --with-stage1-ldflags="-Wl,-rpath,/path/to/lib" was not enough for me to build gcc 4.9.2, bootstrap failed in stage 2. What works is to pass he flags directly to make via
make BOOT_LDFLAGS="-Wl,-rpath,/path/to/lib"
I got this from https://gcc.gnu.org/ml/gcc/2008-09/msg00214.html
While it still involves setting environment variables, what I do is that I define LD_RUN_PATH, which sets the rpath. That way the rest of the system can keep using the system provided libraries instead of using the ones that your gcc build generates.
I am going to make a suggestion that I believe solves your problem, although it definitely does not answer your question. Let's see how many downvotes I get.
Writing a generic wrapper script to set LD_LIBRARY_PATH and then to run the executable is easy; see https://stackoverflow.com/a/7101577/768469.
The idea is to pass something like --prefix=$PREFIX/install to configure, building an install tree that looks like this:
$PREFIX/
install/
lib/
libcloogXX.so
libgmpYY.so
...
bin/
gcc
emacs
...
bin/
.wrapper
gcc -> .wrapper
emacs -> .wrapper
.wrapper is a simple shell script:
#!/bin/sh
here="${0%/*}" # or use $(dirname "$0")
base="${0##*/}" # or use $(basename "$0")
libdir="$here"/../install/lib
if [ "$LD_LIBRARY_PATH"x = x ] ; then
LD_LIBRARY_PATH="$libdir"
else
LD_LIBRARY_PATH="$libdir":"$LD_LIBRARY_PATH"
fi
export LD_LIBRARY_PATH
exec "$here"/../install/bin/"$base" "$#"
This will forward all arguments correctly, handle spaces in arguments or directory names, and so forth. For practical purposes, it is indistinguishable from setting the rpath like you want.
Also, you can use this approach not only for gcc, but for your entire my-personal-$PREFIX tree. I do this all the time in environments where I want an up-to-date suite of GNU tools, but I do not have (or want to admit to have) root access.
Try to add your $PREFIX to /etc/ld.so.conf and then run ldconfig:
# echo $PREFIX >> /etc/ld.so.conf
# ldconfig
This will recreate cache that is used by runtime linker and it will pick up your libraries.
WARNING: This operation will cause ALL applications to use your newly compiled libraries in $PREFIX instead of default location

Including dynamic libraries using automake and autoconf

I am trying to include some dynamic libraries (.so files) for a simple 3D game I am making on linux using C++. These dynamic libraries are for using the Bullet physics engine.
I have very limited knowledge of how use automake and autoconf so any help would be much appreciated.
How might I modify any of the following files to include these dynamic libraries?
Here is my makefile.am:
noinst_HEADERS= BaseApplication.h Physics.h GameApplication.h
bin_PROGRAMS= OgreApp
OgreApp_CPPFLAGS= -I$(top_srcdir)
OgreApp_SOURCES= BaseApplication.cpp Physics.cpp GameApplication.cpp
OgreApp_CXXFLAGS= $(OGRE_CFLAGS) $(OIS_CFLAGS)
OgreApp_LDADD= $(OGRE_LIBS) $(OIS_LIBS)
EXTRA_DIST = buildit makeit
AUTOMAKE_OPTIONS = foreign
Here is my configure.ac:
AC_INIT(configure.ac)
AM_INIT_AUTOMAKE(SampleApp, 0.1)
AM_CONFIG_HEADER(config.h)
AC_LANG_CPLUSPLUS
AC_PROG_CXX
AM_PROG_LIBTOOL
PKG_CHECK_MODULES(OGRE, [OGRE >= 1.2])
AC_SUBST(OGRE_CFLAGS)
AC_SUBST(OGRE_LIBS)
PKG_CHECK_MODULES(OIS, [OIS >= 1.0])
AC_SUBST(OIS_CFLAGS)
AC_SUBST(OIS_LIBS)
AC_CONFIG_FILES(Makefile)
AC_OUTPUT
I also have a buildit file that sets everything in motion:
#!/bin/sh
rm -rf autom4te.cache
libtoolize --force --copy && aclocal && autoheader && automake --add-missing --force-missing --copy --foreign && autoconf
./configure && ./makeit
For Bullet, there should be a bullet.pc file installed on your system which you can use with the autocong macro PKG_CHECK_MODULES, the same way OGRE and OIS are included:
# in configure.ac
PKG_CHECK_MODULES(BULLET, [bullet])
# in Makefile.am
OgreApp_CXXFLAGS= $(OGRE_CFLAGS) $(OIS_CFLAGS) $(BULLET_CFLAGS)
OgreApp_LDADD= $(OGRE_LIBS) $(OIS_LIBS) $(BULLET_LIBS)
If you add more unconditional dependencies, you might want to simplify both files by grouping them like this:
# in configure.ac
PKG_CHECK_MODULES(DEPENDENCIES, [OGRE >= 1.2 OIS >= 1.0 bullet])
# in Makefile.am
OgreApp_CXXFLAGS= $(DEPENDENCIES_CFLAGS)
OgreApp_LDADD= $(DEPENDENCIES_LIBS)
And as Jack Kelly wrote in the comments, if you have pkg-config >= 0.24 (released in 2010), you don't need to use AC_SUBST after PKG_CHECK_MODULE.
In your Makefile.am:
OgreApp_LIBADD= -llib1 -llib2
Does that help?
Edit: or try appending -llib1 -llib2 to the OgreApp_LDADD=... line, I'm not sure myself.