makefile for c++11 doesn't work - c++

I am new to makefiles. I created the following makefile:
CC=g++
DEBUG= -g
CFLAGS=-c -Wall -std=c++11 $(DEBUG)
all: hello
hello: ConsoleApplication1.o clock.o communication.o device_manager.o devices.o
$(CC) ConsoleApplication1.o clock.o communication.o device_manager.o devices.o
ConsoleApplication1.o: ConsoleApplication1.cpp
$(CC) $(CFGLAS) ConsoleApplication1.cpp
clock.o: clock.cpp clock.h
$(CC) $(CFGLAS) clock.cpp
communication.o: communication.cpp
$(CC) $(CFGLAS) communication.cpp
device_manager.o: device_manager.cpp
$(CC) $(CFGLAS) device_manager.cpp
devices.o: devices.cpp
$(CC) $(CFGLAS) devices.cpp
clean:
rm -rf *.o hello
I put the makefile in the directory of all the code files and run the command "make" from the command line.
I got many errors like:
ConsoleApplication1.cpp:494:30: error: ‘chrono’ is not a member of ‘std’
mytime::duration_timer_t timer;
^
ConsoleApplication1.cpp:494:30: error: ‘chrono’ is not a member of ‘std’
ConsoleApplication1.cpp:494:55: error: template argument 1 is invalid
mytime::duration_timer_t timer;
^
ConsoleApplication1.cpp:494:65: error: invalid type in declaration before ‘;’ token
mytime::duration_timer_t timer;
^
ConsoleApplication1.cpp:495:11: error: request for member ‘set_duration’ in ‘timer’, which is of non-class type ‘int’
timer.set_duration(std::chrono::seconds(120));
^
ConsoleApplication1.cpp:495:29: error: ‘std::chrono’ has not been declared
timer.set_duration(std::chrono::seconds(120));
^
ConsoleApplication1.cpp:496:16: error: request for member ‘start’ in ‘timer’, which is of non-class type ‘int’
for( timer.start(); not(timer.expired()); )
When I compile excactly the same file codes in codeBlocks it compiles, so I guess that I have some mistake in the make file.
I have one header file in my project name "general.h" which doesn't have a cpp file. Should I put it also in the makefile?

Your rules say $(CFGLAS), not $(CFLAGS).
That means -std=c++11 is never passed to the compiler, so C++11 features don't exist for you.
Pay more attention to detail!
You will also need to pass $(CFLAGS) to $(CC) in your hello rule, otherwise your compiler invocations will be out of sync and all hell may break loose.

While you compile your object files with -std=c++11 you link them without that flag. Have you tried adding -std=c++11 to hello rule?

Related

‘isnan’ was not declared in this scope error during "make"

I am trying to make g_elpot (https://jugit.fz-juelich.de/computational-neurophysiology/g_elpot) and got the following error:
g++ -O3 -Wno-unused -funroll-all-loops -std=c++11 -fopenmp -I/usr/local/gromacs/include -I/usr/local/fftw/include -c -o dx.o dx.cpp
dx.cpp: In function ‘void write_dx_file(real*, real*, int*, real (*)[3], const char*, unit_t)’:
dx.cpp:41:33: error: ‘isnan’ was not declared in this scope
if (isnan(grid_values[i]))
^
dx.cpp:41:33: note: suggested alternative:
In file included from /usr/local/gromacs/include/gromacs/math/vectypes.h:40:0,
from dx.h:3,
from dx.cpp:1:
/usr/include/c++/5/cmath:641:5: note: ‘std::isnan’
isnan(_Tp __x)
^
<builtin>: recipe for target 'dx.o' failed
make: *** [dx.o] Error 1
I am using g++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609 and cmake version 3.14.0.
My Makefile is:
# Generated automatically from Makefile.in by configure.
#
# This is a Gromacs 3.0 template makefile for your own utility programs.
#
# Copy this file to whatever directory you are using for your own
# software and add more targets like the template one below.
#
# If you are using gmake it is relatively straightforward to add
# an include based on environment variables (like previous Gromacs versions)
# to select compiler flags and stuff automatically, but below it is static:
#
# Variables set by the configuration script:
LIBS = -lgromacs -lfftw3f -lm -fopenmp
LDFLAGS = -L/usr/local/gromacs/lib -L/usr/local/fftw/lib
CPPFLAGS = -I/usr/local/gromacs/include -I/usr/local/fftw/include
CXXFLAGS = -O3 -Wno-unused -funroll-all-loops -std=c++11 -fopenmp
CXX = g++
LD = $(CXX)
g_elpot: g_elpot.o elmap_grid.o spline_interpolation.o fitting.o dx.o spme_grid.o convergence_check.o frame.o result.o units.o molecule.o memory_check.o debug.o
$(LD) $(LDFLAGS) -o $# $^ $(LIBS)
I was trying to run g_elpot and wanted to install it and ran into this issue while running make

How to use Makefile variables as files to include in g++ command?

Here's my problem : I have a makefile file containing the info to compile everything in my project to make my life easier, but it is giving me some headaches recently.
It can compile multiple files, such as this :
objects/io.o: sources/io.cpp
#g++ -c $< -o $# -std=c++11
objects/map.o: sources/map.cpp
#g++ -c $< -o $# -std=c++11
At the top of the makefile, I have variables declared as such :
IO="objects/io.o"
MAP="objects/map.o"
[... other object files ...]
ALL="$(IO) $(MAP) [...]"
When I want to compile my main file, I use this command :
main.exe: tests/main.cpp
#g++ $< $(ALL) -o $# -std=c++11
When I compile this problem manually (inputting everything in one line of command, instead of making make main.exe) it compiles without problems.
However, when I use the make command for the project, the following error pops up :
clang: error: no such file or directory: 'objects/io.o objects/map.o [...]'
make: *** [main.exe] Error 1
Can I not use variables this way ? I'm extremely confused. I know for a fact those files are compiled, it just seems the make utility doesn't understand file paths.
I think the problem is that you have quoted your variables. Try unquoting them and they won't expand a single parameter:
ALL = $(obj1) $(obj2) ...
Also, for those objects which use the same compilation process I generally define a single rule:
obj/%.o: %.cc
$(GCC) -o $# -c $< $(FLAGS)
Obviously that would require defining the extra variables GCC and FLAGS

C++ compilation using mpic++ error

I'm trying to compile a program using mpic++ on a Linux system. The program contains source files provided by our professor and it's these files that throw the errors. In Visual Studio C++ there's no problem compiling the code but we are requiered to launch it on a Linux system, and when I move it to the Linux system to compile it using mpic++ an error occurs.
mpic++ -std=c++11 -Wall -c CommonApprox.cpp
In file included from ApproxIface.h:3:0,
from CommonApprox.h:3,
from CommonApprox.cpp:1:
referencedIface.h:23:19: error: '__stdcall' declared as a 'virtual' field
referencedIface.h:23:19: error: expected ';' at end of member declaration
referencedIface.h:23:90: error: ISO C++ forbids declaration of 'QueryInterface' with no type [-fpermissive]
referencedIface.h:24:17: error: '__stdcall' declared as a 'virtual' field
referencedIface.h:24:17: error: expected ';' at end of member declaration
referencedIface.h:24:17: error: redeclaration of 'ULONG IReferenced::__stdcall'
referencedIface.h:23:19: note: previous declaration 'HRESULT IReferenced::__stdc all'
The erorrs go on and on, these are just the first few. The code this particular error references is shown below.
#define IfaceCalling __stdcall
class IReferenced {
/* Actually, this is IUnknown of the WinAPI's COM
HRESULT and ULONG are used to allow possible and easy interoperability
accross different compilers and languages on Windows
*/
public:
virtual HRESULT IfaceCalling QueryInterface(/*REFIID */ void* riid, void ** ppvObj) = 0;
virtual ULONG IfaceCalling AddRef() = 0;
virtual ULONG IfaceCalling Release() = 0;
};
As far as I know, we are not allowed to alter the code that was provided to us.
Thank you for you help.
EDIT:
I use a makefile to compile the program.
OBJECTS = CommonApprox.o DatabaseHandler.o MaskHandler.o Output.o StatsHandler.o GlucoseLevels.o AkimaInterpolation.o CatmullRomInterpolation.o ApproxIface.o referencedImpl.o main.o
CFLAGS = -std=c++11 -Wall
LIBS = -lsqlite3 -ltbb
CC = mpic++
all: ku_ppr_distr
ku_ppr_distr : $(OBJECTS)
$(CC) $(CFLAGS) $^ $(LIBS) -o $#
rm -f *.o
CommonApprox.o: CommonApprox.cpp CommonApprox.h hresult.h
$(CC) $(CFLAGS) -c CommonApprox.cpp
DatabaseHandler.o: DatabaseHandler.cpp DatabaseHandler.h
$(CC) $(CFLAGS) -c DatabaseHandler.cpp
MaskHandler.o: MaskHandler.cpp MaskHandler.h
$(CC) $(CFLAGS) -c MaskHandler.cpp
Output.o: Output.cpp Output.h
$(CC) $(CFLAGS) -c Output.cpp
StatsHandler.o: StatsHandler.cpp StatsHandler.h
$(CC) $(CFLAGS) -c StatsHandler.cpp
GlucoseLevels.o: GlucoseLevels.cpp GlucoseLevels.h
$(CC) $(CFLAGS) -c GlucoseLevels.cpp
AkimaInterpolation.o: AkimaInterpolation.cpp AkimaInterpolation.h
$(CC) $(CFLAGS) -c AkimaInterpolation.cpp
CatmullRomInterpolation.o: CatmullRomInterpolation.cpp CatmullRomInterpolation.h
$(CC) $(CFLAGS) -c CatmullRomInterpolation.cpp
ApproxIface.o: ApproxIface.cpp ApproxIface.h
$(CC) $(CFLAGS) -c ApproxIface.cpp
referencedImpl.o: referencedImpl.cpp referencedImpl.h
$(CC) $(CFLAGS) -c referencedImpl.cpp
main.o: main.cpp
$(CC) -w $(CFLAGS) -c main.cpp
%.o: %.c
$(CC) -c $<
clean:
rm -f *.o
mpic++ is a wrapper for g++ (the GNU C++ compiler on Linux). __stdcall is a Visual C++ specific compiler directive to control the generated code which is not recognised by g++. You might try adding a null definition for __stdcall to your makefile, i.e.
CFLAGS = -D__stdcall= -std=c++11 -Wall
That should get rid of quite a few error messages.

G++ does not seem to recognize -std=c++11

I compile my code with the flag -std=c++11 given, and I get all kinds of errors depicting that I should use the same flag. Also, auto is not recognised being a type.
Makefile:
GCCPATH = /path/gcc/5.3.0
CC = $(GCCPATH)/bin/g++
DARGS = -ggdb #debug arguments
CARGS = -std=c++11 #C arguments
WARGS = -Wall -Wextra #warning arguments
AARGS = $(DARGS) $(CARGS) $(WARGS) #all arguments
GCCLIBPATH = $(GCCPATH)/lib64
LIBS = -l curl
LIBD = -L $(GCCLIBPATH) -Wl,-rpath=$(GCCLIBPATH)
.PHONY: webspider
webspider: ../title/htmlstreamparser.o filesystem.o
$(CC) $(AARGS) -o $# $#.cpp $+ $(LIBS) $(LIBD)
filesystem:
$(CC) $(AARGS) -c $#.cpp
The warnings and errors I get:
warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
warning: range-based ‘for’ loops only available with -std=c++11 or -std=gnu++11
error: ‘weblink’ does not name a type
for(auto weblink: weblinks)
Now my question is: What should I do to make g++ recognise this clearly given flag?
I also tried to replace it with -std=c++0x, to no avail.
EDIT:
Full output of make:
g++ -c -o filesystem.o filesystem.cpp
In file included from filesystem.cpp:1:0:
filesystem.hpp:23:36: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
std::string dir = getCurrentPath();
^
filesystem.cpp: In member function ‘std::__cxx11::string Filesystem::createMD5(std::__cxx11::string)’:
filesystem.cpp:49:19: warning: range-based ‘for’ loops only available with -std=c++11 or -std=gnu++11
for(long long c: result)
^
filesystem.cpp: In member function ‘void Filesystem::createLinkIndex(std::__cxx11::string, strVec)’:
filesystem.cpp:57:11: error: ‘weblink’ does not name a type
for(auto weblink: weblinks) {
^
filesystem.cpp:61:1: error: expected ‘;’ before ‘}’ token
}
^
filesystem.cpp:61:1: error: expected primary-expression before ‘}’ token
filesystem.cpp:61:1: error: expected ‘;’ before ‘}’ token
filesystem.cpp:61:1: error: expected primary-expression before ‘}’ token
filesystem.cpp:61:1: error: expected ‘)’ before ‘}’ token
filesystem.cpp:61:1: error: expected primary-expression before ‘}’ token
make: *** [filesystem.o] Error 1
The problem is you do not specify all your dependencies, in particular how to build all your intermediate object files.
So what happens is make makes up its own rules and invisibly sneaks them in while you're not looking.
The way to control these implicit rules is through setting the correct predefined variables:
CXX := $(GCCPATH)/bin/g++ # c++ compiler
CPPFLAGS := -I/path/to/headers # preprocessor flags
CXXFLAGS := -std=c++11 # compiler flags
LDFLAGS := -L/path/to/libs # linker flags
LDLIBS := -lcurl # libraries to link
# etc...
By using the correct predefined variables, rather than making up your own, you can save a lot of work when building a Makefile.
In the end, based on the comments, it was fixed by changing
filesystem:
$(CC) $(AARGS) -c $#.cpp
to
filesystem.o: filesystem.cpp
$(CC) $(AARGS) -c $+
as Makefile did not understand that I was trying to make filesystem.o with the rule filesystem: .... When stating this explicitely, it worked as intended.
Advantage of this method over the answer of Galik is the ability to use own variables, albeit in this case not that much of an advantage since it is a small project.

g++ errors while compiling object files

I wrote a small makefile which follows the general structure, creating object files and then linking to create an executable. Here is how it looks..
CXX=g++
CXXFLAGS=-Wall -g
INCLUDES= -I ./
LDFLAGS= -L ./
LIBS= -lcryptopp
SRCS= test.cpp
OBJS= $(SRCS:.cpp=.o)
EXEC=test
all: $(EXEC)
$(EXEC): $(OBJS)
$(CXX) $(CXXFLAGS) $(INCLUDES) -o $(EXEC) $(OBJS) $(LDFLAGS) $(LIBS)
.cpp.o:
$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $#
Cryptopp library(cryptopp) is static. Now when I try to run this makefile, when the first command which tries to create object file runs.. its gives me many errors like this..
test.cpp:289: instantiated from here
./include/algparam.h:322: warning: unused variable 'p'
./include/algparam.h: In member function 'void CryptoPP::AlgorithmParametersTemplate<T>::MoveInto(void*) const [with T = unsigned char]':
In the end, it links all fine and the executable works but how can I get rid of those warnings without removing -wall? I don't have much experience with make and makefiles.
That's not an error, it's a warning. (Technically, you can consider warnings as errors that don't prevent the compiler from finishing its job.)
And the way you fix it is to fix your code. This has nothing to do with the makefile. Delete the variable 'p' from line 322 in ./include/algparam.h. (There was a bit of a hint in the warning message from the compiler.)
for this warning, you can just comment variable p in test.cpp or .h file, because you don't use it, or like this
in your code
{
...
#ifdef _DEBUG_
xxx p;
#endif
...
}
and in your makefile, if you want to use p, just add -D_DEBUG_ in your CXXFLAGS