How do I stop autotools from adding command line options? - c++

In my source directory, I type make main.o, which issues the command:
g++ -DHAVE_CONFIG_H -I. -I.. -std=gnu++11 -fpermissive -Wno-write-strings -O0 -g -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.cc
mv -f .deps/main.Tpo .deps/main.Po
Some of this was created by me (-std=gnu++11 -fpermissive -Wno-write-strings -O0 -g) and some of it wasn't (-g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo)
Q1: How do I tell autotools to stop inserting -g -O2?
Q2: What's the deal with -MT main.o -MD -MP -MF .deps/main.Tpo? Is it necessary? Unnecessary? Can I get rid of it? If so, how?
Q3: How do I add to AM_CXXFLAGS via m4 macros seemlessly? The approach I have adopted so far is to define the macro m4/ax_debug.m4 as follows:
AC_DEFUN([AX_DEBUG], [
AC_ARG_WITH([debug],
[AS_HELP_STRING([--with-debug], [Enable debug (default is no)])],
[with_debug=$withval],
[with_debug=no])
AM_CONDITIONAL([WITH_DEBUG], [test "x$with_debug" = "xyes"])
AS_IF([test "x$with_debug" = "xyes"], [
AC_SUBST([DEBUG_CXXFLAGS], ["-O0 -g"])
])
AC_MSG_NOTICE([checking if debugging required ... $with_debug])
])dnl AX_DEBUG
and to use those flags within src/Makefile.am as follows:
AM_CXXFLAGS = ... $(DEBUG_CXXFLAGS)
Is there a way I can arrange it so that DEBUG_CXXFLAGSis added automatically?
Update (with possible answers): In regards to Q1, the answer appears to be to add the following line to configure.ac after AC_INIT(...):
: ${CXXFLAGS=""}
and delete $(DEBUG_CXXFLAGS) from src/Makefile.am.
In regards to Q2, I still have no idea what that's about.
In regards to Q3, the answer would appear to be to write
AC_SUBST([CXXFLAGS], ["$CXXFLAGS -O0 -g"])
instead of
AC_SUBST([DEBUG_CXXFLAGS], ["-O0 -g"])
I'm not sure if that's an "official" solution though.

Related

Quickly Build Package Repeatedly in R

I have a package I'm writing in R that has a boatload of complicated C++ code which takes a while to compile.
When I change the code, I'd like to quickly rebuild the package so I can test it. However, R CMD build seems to start from scratch each time, rather than using my code's makefiles to do only what is needed.
Is there a way to quickly do repeated builds of a package in R for testing?
I am a little overdue a short blog post on this, but I mentioned it a couple of times before: use ccache. It helps dramatically when the files don't change (ie when you just alter help pages), or when few files changes. Caching is a very clever trick, and package is robust.
On Ubuntu/Debian: sudo apt-get install ccache followed by e.g. this in your ~/.R/Makevars:
VER=
CCACHE=ccache
#CCACHE=
CC=$(CCACHE) gcc$(VER)
CXX=$(CCACHE) g++$(VER)
CXX11=$(CCACHE) g++$(VER)
CXX14=$(CCACHE) g++$(VER)
That also allows to switch between g++ versions. Changing it to clang++ is left as an exercise to the reader ;-)
Besides this, see the options to R CMD build and R CMD INSTALL to skip vignette and/or manual building to further speed up re-builds.
Illustration: Here is re-install of Rcpp itself (fresh from a git pull) where the first installation takes 21.9 seconds on my (decent) machine at work, the second then only takes 1.4 seconds thanks to ccache:
~/git/rcpp(master)$ time R CMD INSTALL .
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘Rcpp’ ...
** libs
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/ -fpic -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c Date.cpp -o Date.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/ -fpic -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c Module.cpp -o Module.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/ -fpic -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c Rcpp_init.cpp -o Rcpp_init.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/ -fpic -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c api.cpp -o api.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/ -fpic -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c attributes.cpp -o attributes.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/ -fpic -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c barrier.cpp -o barrier.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o Rcpp.so Date.o Module.o Rcpp_init.o api.o attributes.o barrier.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/Rcpp/libs
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (Rcpp)
real 0m21.917s
user 0m21.388s
sys 0m2.304s
~/git/rcpp(master)$ ./cleanup
~/git/rcpp(master)$ time R CMD INSTALL .
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘Rcpp’ ...
** libs
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/ -fpic -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c Date.cpp -o Date.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/ -fpic -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c Module.cpp -o Module.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/ -fpic -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c Rcpp_init.cpp -o Rcpp_init.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/ -fpic -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c api.cpp -o api.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/ -fpic -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c attributes.cpp -o attributes.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/ -fpic -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c barrier.cpp -o barrier.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o Rcpp.so Date.o Module.o Rcpp_init.o api.o attributes.o barrier.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/Rcpp/libs
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (Rcpp)
real 0m1.444s
user 0m1.380s
sys 0m1.452s
~/git/rcpp(master)$
Dirk's answer above pointed me in the right direction, but was insufficient. Since he has requested that I not append the final steps I required, I do so here.
My question could not have been answered without the following.
Dirk's answer did not work off the bat for me. If compilation still seems slow to you, try running:
ccache -s
The result will look a little like this
cache directory /home/myuser/.ccache
primary config /home/myuser/.ccache/ccache.conf
secondary config (readonly) /etc/ccache.conf
cache hit (direct) 0
cache hit (preprocessed) 0
cache miss 989
cache hit rate 0 %
called for link 12
preprocessor error 12
cleanups performed 16
files in cache 177
cache size 31.1 MB
max cache size 5.0 GB
Note that cache isn't getting hit, which means ccache isn't doing anything.
You can use:
export CCACHE_LOGFILE=ccache.log
R CMD build .
to do some debugging, though this wasn't helpful for me.
What fixed things was to run:
export CCACHE_NOHASHDIR=true
R CMD build .
As it turns out, ccache sometimes takes the location of a file into account. R CMD build . appears to build in a temporary directory, so the location of the files was changing each time.

How to add -std=c++0x into the eclipse ubuntu?

I am trying to add -std=c++0x into the eclipse Ubuntu but I do not know how to add it.
My original set up:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/copy2.d" -MT"src/copy2.d" -o "src/copy2.o" "../src/copy2.cpp"
Does not include -std=c++0x that's why giving me the error when I run std::stoi:
error: stoi is not a member of std
My expectation:
g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 MMD -MP -MF"src/copy2.d" -MT"src/copy2.d" -o "src/copy2.o" "../src/copy2.cpp"

How do you keep Netbeans 8.0 from rebuilding every file?

For all of my c++ projects using netbeans, whenever I make a single small change the entire project is rebuilt. There must be a way to prevent this.
My build logs ends up looking like this:
mkdir -p build/Debug/CLang-Windows/_ext/237546996
rm -f "build/Debug/CLang-Windows/_ext/237546996/Sensor.o.d"
clang++ -g -std=gnu++11 -c -g -DSFML_STATIC -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers/libsndfile/windows -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers/libfreetype/windows -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers/jpeg -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers/AL -I../SFML/src -I../SFML/include -MMD -MP -MF "build/Debug/CLang-Windows/_ext/237546996/Sensor.o.d" -o build/Debug/CLang-Windows/_ext/237546996/Sensor.o ../SFML/src/SFML/Window/Sensor.cpp
mkdir -p build/Debug/CLang-Windows/_ext/237546996
rm -f "build/Debug/CLang-Windows/_ext/237546996/SensorManager.o.d"
clang++ -g -std=gnu++11 -c -g -DSFML_STATIC -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers/libsndfile/windows -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers/libfreetype/windows -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers/jpeg -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers/AL -I../SFML/src -I../SFML/include -MMD -MP -MF "build/Debug/CLang-Windows/_ext/237546996/SensorManager.o.d" -o build/Debug/CLang-Windows/_ext/237546996/SensorManager.o ../SFML/src/SFML/Window/SensorManager.cpp
mkdir -p build/Debug/CLang-Windows/_ext/237546996
rm -f "build/Debug/CLang-Windows/_ext/237546996/Touch.o.d"
clang++ -g -std=gnu++11 -c -g -DSFML_STATIC -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers/libsndfile/windows -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers/libfreetype/windows -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers/jpeg -I/C/Users/izack_000/Documents/Programming/Projects/SynapseCpp/SFML/extlibs/headers/AL -I../SFML/src -I../SFML/include -MMD -MP -MF "build/Debug/CLang-Windows/_ext/237546996/Touch.o.d" -o build/Debug/CLang-Windows/_ext/237546996/Touch.o ../SFML/src/SFML/Window/Touch.cpp
As as you can see the object files are being removed before generating new files. Is there a setting for this or do I have to change the makefiles generated by netbeans?
Or perhaps there is something I accidentally turned on? (always clean?) Though I don't think its likely because its happening for both of my projects.
Edit: this seems to only happens on windows

Set CXXFLAGS in Rcpp Makevars

I would like to set the C++ compiler flag to -O0 in the Makevars of an Rcpp project.
If I take a look at /etc/R/Makeconf, I see that the compilation command seems to be
$(CXX) $(ALL_CPPFLAGS) $(ALL_CXXFLAGS) -c $< -o $#
Since
ALL_CXXFLAGS = $(R_XTRA_CXXFLAGS) $(PKG_CXXFLAGS) $(CXXPICFLAGS) $(SHLIB_CXXFLAGS) $(CXXFLAGS)
I can edit in the Makevars the variable $(PKG_CXXFLAGS) to add headers for specific libraries, but I am not satisfied with CXXFLAGS = -O3 -pipe -g $(LTO). I would also like to be able to do that directly in the Makevars, so as to tune each project according to my needs.
When I edit CXXFLAGS in the Makevar, nothing happens. Is is possible to adjust that variable ? Is another approach possible ? I know that I can edit ~/.R/Makevars, and switch as requested. I wondered if there was a more robust approach.
You generally want the PKG_* variants in your local file, e.g. ~/.R/Makevars.
Here is a (shortened, edited) portion of mine:
## for C code
CFLAGS= -O3 -g0 -Wall -pipe -pedantic -std=gnu99
## for C++ code
#CXXFLAGS= -g -O3 -Wall -pipe -Wno-unused -pedantic -std=c++11
CXXFLAGS= -g -O3 -Wall -pipe -Wno-unused -pedantic
## for Fortran code
#FFLAGS=-g -O3 -Wall -pipe
FFLAGS=-O3 -g0 -Wall -pipe
## for Fortran 95 code
#FCFLAGS=-g -O3 -Wall -pipe
FCFLAGS=-O3 -g0 -Wall -pipe
VER=-4.8
CC=ccache gcc$(VER)
CXX=ccache g++$(VER)
SHLIB_CXXLD=g++$(VER)
FC=ccache gfortran
F77=ccache gfortran
MAKE=make -j8
The other (system-global) approach is to create and edit /etc/R/Makeconf.site (or, when /etc/R/ does not exist, $RHOME/etc/R/Makeconf.site.

Weird error " multiple definition of `xxx` " while compiling C++ project

When I try to compile my C++ project via my Makefile I keep getting errors like those:
Server.o: In function `Bot::getRandomMessage()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: multiple definition of `Bot::getRandomMessage()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: first defined here
Server.o: In function `Bot':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:27: multiple definition of `Bot::Bot()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:27: first defined here
Server.o: In function `~Bot':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: multiple definition of `Bot::~Bot()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: first defined here
Server.o: In function `~Bot':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: multiple definition of `Bot::~Bot()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: first defined here
Server.o: In function `~Bot':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: multiple definition of `Bot::~Bot()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: first defined here
Server.o: In function `Bot::getName()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:33: multiple definition of `Bot::getName()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:33: first defined here
Server.o: In function `ChatRoom::getCurrentTime()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Server.cpp:74: multiple definition of `ChatRoom::getCurrentTime()'
main.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Server.cpp:74: first defined here
Server.o: In function `Bot::getRandomMessage()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: multiple definition of `vectorOfThreads'
main.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Server.cpp:74: first defined here
Server.o: In function `Bot::getRandomMessage()':
I'm quite confused with that.. When I compile it directly with the command
g++ main.cpp -Wall -pedantic -Wno-long-long -O0 -ggdb -lncurses -pthread -o AppName , then it doesn't produce any error. So I expect, that the error appears somewhere in my Makefile
#macros
Remove=rm -rf
Doxygen=Doxyfile
RUN=./dvoram64
FLAGS=-Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g
OBJECTS=main.o Bot.o Server.o Client.o GrumpyBot.o JokerBot.o WeatherBot.o DummyBot.o
#generates final binary and documentation
all: $(Doxygen)
make compile
#build into final binary
compile: $(RUN)
#run program
run:
make link
$(RUN)
$(RUN)
clean:
$(Remove) dvoram64
$(Remove) $(OBJECTS)
#generate documentation in '<login>/doc' folder
doc: $(Doxygen) /*
( cd ./ | doxygen $(Doxygen))
link: $(OBJECTS)
g++ $(OBJECTS) -lncurses -pthread -o dvoram64
#rules how to compile into the executalble file
$(RUN): $(OBJECTS)
Bot.o: ./Bot.cpp ./Bot.h
g++ $(FLAGS) -c ./Bot.cpp
DummyBot.o: ./DummyBot.cpp ./DummyBot.h ./Bot.h
g++ $(FLAGS) -c ./DummyBot.cpp
GrumpyBot.o: ./GrumpyBot.cpp ./GrumpyBot.h ./Bot.h
g++ $(FLAGS) -c ./GrumpyBot.cpp
JokerBot.o: ./JokerBot.cpp ./JokerBot.h ./Bot.h
g++ $(FLAGS) -c ./JokerBot.cpp
WeatherBot.o: ./WeatherBot.cpp ./WeatherBot.h ./Bot.h
g++ $(FLAGS) -c ./WeatherBot.cpp
Client.o: ./Client.cpp
g++ $(FLAGS) -c ./Client.cpp
main.o: ./main.cpp
g++ $(FLAGS) -c ./main.cpp
Server.o: ./Server.cpp ./Bot.h ./JokerBot.h ./WeatherBot.h ./GrumpyBot.h ./DummyBot.h
g++ $(FLAGS) -c ./Server.cpp
Could anybody please explain me, what causes this error and show me, how to fix it?
Look at what the error messages are telling you. Start with the first lines:
Server.o: In function `Bot::getRandomMessage()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: multiple definition of `Bot::getRandomMessage()'
This message says that the object file Server.o contains a multiple definition of the function Bot::getRandomMessage() function, and that the multiple definition comes from line 18 in the source file Bot.cpp. Now look at the next line:
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: first defined here
This tells you that the definition in Server.o is a multiple definition because there is also a definition in Bot.o. It also tells you the definition in Bot.o came from line 18 in the source file Bot.cpp, which is the same place in the source as the other definition.
This means that Bot.cpp was compiled at least twice, once to make Server.o and once to make Bot.o.
That is probably not what you want. It suggests that some of your source or header files include Bot.cpp when you meant to include Bot.h, or that you have otherwise included Bot.cpp incorrectly. Another possibility is that you have a compile command that compiles Bot.cpp to make Server.o.
generally when i face something like this ...... its a double rule occurrence or the project environment is messed up but Bro , this ain't a makefile issue.
you will have to look into the code ..... I simulated and tested the makefile you put in the question here , with empty files and echo . The makefile seems to be working a-ok.
Kaizen ~/so_test $ make -nf mk.t2
make compile
Kaizen ~/so_test $ make -nf mk.t2 compile
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./main.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./Bot.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./Server.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./Client.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./GrumpyBot.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./JokerBot.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./WeatherBot.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./DummyBot.cpp
i cant deduce much of a suggestion based on whats there , srry ...
When you compile with g++ main.cpp -Wall -pedantic -Wno-long-long -O0 -ggdb -lncurses -pthread -o AppName you actually don't include your Server, Client, Bot, DummyBot etc. (That you have in your makefile). Thats why you don't see that error.
also If main.cpp compiles without any other files then why do you need these Client, Bot, Server etc .. in your makefile ?
There must be a redefinition somewhere. try to clean and recompile. and then check the functions that its reporting. like Server.cpp:74, Bot.cpp:18, Bot::getRandomMessage()
also surprisingly your main.cpp doesn't call any Server, Bot ... functions. If it calls its supposed to throw linker errors.