C++ compilation using mpic++ error - c++

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.

Related

Compiling multiple c++ files doesn't work with certain file names

I am trying to compile multiple .cpp files, using a Makefile with make. I have a .cpp file containing the main function, in the other .cpp file a basic class (for every doubt I'll put all the code i'm using down here).
Example of names used to make it compile or to make it not compile:
-it works by having them named "prova.cpp"(contains the main function) and "pa.cpp"(contains the class) (below the commands done by the Makefile)
gioele#GioPC-U:~/Dev/Cpp/Exercises/666prova$ make
g++ -g -Wall -c src/prova.cpp -o obj/prova.o
g++ -g -Wall -c src/pa.cpp -o obj/pa.o
g++ -g -Wall obj/prova.o -o bin/provaBin
-it doesn't work by having them named "ciao.cpp"(contains the main function) and "pa.cpp"(contains the class)
gioele#GioPC-U:~/Dev/Cpp/Exercises/666prova$ make
g++ -g -Wall -c src/pa.cpp -o obj/pa.o
g++ -g -Wall -c src/ciao.cpp -o obj/ciao.o
g++ -g -Wall obj/pa.o -o bin/provaBin
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [Makefile:15: bin/provaBin] Errore 1
I think the problem is with the order of the files inside the Makefile. When it doesn't work is because is trying to get a binary from the .o without the main function. No idea on how to resolve.
The file with the main method:
#include <iostream>
int main() {
std::cout << "Hello world!" << std::endl;
return 0;
}
The class file:
class Class {
public:
int x;
int y;
};
The Makefile (still learning to work with Makefiles, probably the problem is here, any advice to make a Makefile of this kind better is appreciated, or if I am using something not properly):
CC=g++
CFLAGS=-g -Wall
OBJ=obj
SRC=src
BINDIR=bin
BIN=$(BINDIR)/provaBin
SRCS=$(wildcard $(SRC)/*.cpp)
OBJS=$(patsubst $(SRC)/%.cpp, $(OBJ)/%.o, $(SRCS))
all: $(BIN)
$(BIN): $(OBJS)
$(CC) $(CFLAGS) $< -o $#
$(OBJ)/%.o: $(SRC)/%.cpp
$(CC) $(CFLAGS) -c $< -o $#
clean:
$(RM) $(BINDIR)/* $(OBJ)/*
Thank you in advance.
You can fix this by changing one character.
In this rule:
$(BIN): $(OBJS)
$(CC) $(CFLAGS) $< -o $#
the prerequisite list $(OBJS) expands to a list of object files, such as prova.o pa.o or pa.o ciao.o. You want Make to incorporate that list into the linking command, like this:
g++ -g -Wall prova.o pa.o -o bin/provaBin
But the automatic variable $< expands to only the first item in the prerequisite list. If the first item is an object file that does not contain the main() function, such as pa.o, the linker will complain that main() is missing. (If it is an object file that contains main(), such as prova.o, then you will not get that error, but you may still have problems.)
The solution is to use the automatic variable $^, which expands to the complete list of prerequisites:
$(BIN): $(OBJS)
$(CC) $(CFLAGS) $^ -o $#

G++ unresolved identifier, cannot find linker error

I'm not sure if this is an error in my makefile, header, or source, but it looks like all the relevant pieces of code should link up nicely so I can use functions from one C++ file inside another, but I'm running into a brick wall. Here's a simplified version of what I'm working with:
common.h:
//common.h
#ifndef COMMON_H
#define COMMON_H
int foo();
#endif
common.cc:
//common.cc
#include "common.h"
int main(){
int z = foo();
return 0;
}//main
int foo(){
int x = 5;
int y = 7;
return x + y;
}//foo
test.cc:
//test.cc
#include "common.h"
int main(){
return foo();
}
And the makefile (sorry, it's a bit more complex, to better reflect how my overall project is operating):
TARGETS = common test
FLAGS = -lpthread
DEPS = common.h
all: $(TARGETS)
common: common.cc $(DEPS)
g++ $^ $(FLAGS) -g -o $#
test: test.cc $(DEPS)
g++ $(FLAGS) $^ -g -o $#
clean::
rm -fv $(TARGETS) *~
The compiler seems happy compiling common.cc, but runs into an unresolved identifier error on test.cc:
g++ -lpthread test.cc common.h -g -o test
/tmp/ccMwBGAj.o: In function `main':
/home/...../test.cc:6: undefined reference to `foo()'
Am I missing something here?
Thanks!
First, notice that when you try to build test, the only files you use are test.cc and common.h. The code in test.cc calls the function foo(), but that function is not defined in either of those files; it is defined in common.cc, which was not invited. And if you try to fix that by adding common.cc or common.o to the recipe, you'll run into more trouble, because common.cc contains a definition of main(), and so does test.cc, and there can be only one.
If you want to use foo() with other versions of main(), you should not put a main() in common.cc.
Now for the makefile recipe:
test: test.cc $(DEPS)
g++ $(FLAGS) $^ -g -o $#
This expands to:
test: test.cc common.h
g++ -lpthread test.cc common.h -g -o test
Which is incorrect, as #NeilButterworth has pointed out. You could do this:
test: test.cc common.cc
g++ test.cc common.cc -lpthread -g -o test
which can be written as:
test: test.cc common.cc
g++ $^ $(FLAGS) -g -o $#
But that can fail to rebuild when common.h is changed, and when it does rebuild it can recompile a source that hasn't changed. A better approach is:
common.o: common.cc $(DEPS)
g++ -c $< -g -o $#
test.o: test.cc $(DEPS)
g++ -c $< -g -o $#
common: common.o
g++ $^ $(FLAGS) -o $#
test: test.o common.o
g++ $^ $(FLAGS) -o $#
And further improvement is possible, once you have this much working.

how do I set compilingflags in a makefile?

I'm used to program in IDEs, but switched to vim and plugins recently. Now I try to write a makefile for a c++ project, but somehow if I run make I always get the error
g++ -c -o *.o createOutput.cpp
In file included from /usr/include/c++/4.8/thread:35:0,
from createOutput.cpp:5:
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
^
This is my makefile:
CC = clang++
# compiler flags
CFLAGS = -O3 -Wall -Werror -std=c++11
CFLAGS_SFML = -lsfml-graphics -lsfml-window -lsfml-system
all: program.exe clean
program.exe: *.o
$(CC) -o program.exe *.o $(CFLAGS) $(CFLAGS_SFML)
getInput.o: getInput.cpp
$(CC) -c getInput.cpp $(CFLAGS)
createOutput.o: createOutput.cpp
$(CC) -c createOutput.cpp $(CFLAGS)
main.o: main.cpp
$(CC) -c main.cpp $(CFLAGS)
.PHONY: clean
clean:
rm *.o
#echo clean done
Where is my error? Why is it using g++ instead of clang? And why isn't it using the -std=c++11 parameter? Sorry for the beginner questions, I unfortunately can't find a solution with google.
You want to set CXXFLAGS, that gets picked up automatically by make (and sent to your compiler (eg g++, clang++, etc).
make tried to make target '*.o'.
So, instead of that, you can specify sources list explicitly:
CC = clang++
#compiler flags
CFLAGS = -O3 -Wall -Werror -std=c++11
CFLAGS_SFML = -lsfml-graphics -lsfml-window -lsfml-system
SRCS = getInput.cpp createOutput.cpp main.cpp
OBJS = $(SRCS:.cpp=.o)
all: program.exe
program.exe: $(OBJS)
$(CC) -o program.exe *.o $(CFLAGS) $(CFLAGS_SFML)
getInput.o: getInput.cpp
$(CC) -c getInput.cpp $(CFLAGS)
createOutput.o: createOutput.cpp
$(CC) -c createOutput.cpp $(CFLAGS)
main.o: main.cpp
$(CC) -c main.cpp $(CFLAGS)
.PHONY : clean
clean:
rm *.o
#echo clean done
Note definition of variables OBJS and SRCS.

g++ consolidation: ld: fatal: file main.o: unknown file type

I'm scanning the web and all my project files for solution but still can't find the answer why my linker won't finish the job. Everything smoothly compiles into .o files, but the last make command fails. And here is the Makefile content:
CXX = g++
CXXFLAGS = -Wall -pedantic -c
OBJS = main.o operacje.o porownaj.o
dzialania: $(OBJS)
$(CXX) $^ -o $#
main.o: main.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) $^ -o $#
operacje.o: operacje.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) $^ -o $#
porownaj.o: porownaj.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) $^ -o $#
clean:
rm -f *o
and again, here is the mistake that pops out:
g++ main.o operacje.o porownaj.o -o dzialania
ld: fatal: file main.o: unknown file type
ld: fatal: file processing errors. No output written to dzialania
*** Error code 1
make: Fatal error: Command failed for target `dzialania'
I'm sure it's some kind of a basic mistake but after staring at the file for a few hours I won't notice it anyway. Maybe some of you folks with notice the bug with a fresh eye.
btw. it's my first post after long-term passive lurking, I hope I did everything right. Thanks in advance!
#edit1 OK, I did all the suggested corrections
#edit2 Seems like the problem is caused by improper module division of my program. I'll rearrange it's structure and let you know if it works then. Thanks for all the support!
#edit3 OK, I changed the structure of my program and everything runs smooth, Thanks again!
Try using $< instead of $^ in your rules to compile main.o, operacje.o, and porownaj.o:
main.o: main.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) $< -o $#
operacje.o: operacje.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) $< -o $#
porownaj.o: porownaj.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) $< -o $#
That will cause make to compile only the corresponding .cpp file. When you use $^ the header files are passed to the g++ command which tells the compiler to create precompiled headers for them - that's what's ending up in main.o instead of the object file for main.cpp.
GNU make variable definitions like CC = g++, or CFLAGS = -Wall -pedantic etc.. should each be on its own line:
CC = g++
CFLAGS = -Wall -pedantic
OBJS = main.o operacje.o porownaj.o
BTW, you probably mean
CXX = g++
CXXFLAGS = -Wall -pedantic
You certainly don't want -c explicitly in your CFLAGS or CXXFLAGS; you really should remove it.
Also, recipes should be after its rule, so you want
dzialania: $(OBJS)
$(LINK.cc) $^ -o $#
operacje.o: operacje.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) -c $< -o $#
The several spaces are actually a single tab character.
Run make -p to understand the rules known by make; see also this answer and that one.
Take time to read GNU make documentation.

Makefile for simple C++ OpenGL program

I'm trying to write a makefile for an OpenGL program written in C++ (OSX).
Right now, there is only the single file chess.cpp, but I expect to add other files to the project, so I'm trying to create a makefile that I can expand to handle new files as needed. I'm getting these errors:
clang: error: no such file or directory: 'chess.o'
clang: warning: -framework GLUT: 'linker' input unused
clang: warning: -framework OpenGL: 'linker' input unused
make: *** [chess.o] Error 1
This is the makefile that I created. It's borrowed from something that I normally use for C programs, so if it looks strange, that could be why. How can I make this work for my C++ project?
CC = g++
CFLAGS = -c -g -Wall -Wextra
DEPS =
LDFLAGS = -framework GLUT -framework OpenGL
all: chess
%.o: %.cpp $(DEPS)
$(CC) -c -o $# $< $(CFLAGS)
chess.o: chess.cpp
$(CC) -c chess.cpp chess.o $(LDFLAGS)
chess: chess.o
$(CC) -o chess.o (LDFLAGS)
clean:
rm chess
rm *.o
There's a couple of errors:
First:
%.o: %.cpp $(DEPS)
$(CC) -c -o $# $< $(CFLAGS)
This compiles all .cpp files and everything in $(DEPS) into their respective .o files.
This makes the following line redundant: (although this line appends the linker flags, which the previous does not)
chess.o: chess.cpp
$(CC) -c chess.cpp chess.o $(LDFLAGS)
Even so, the line has an error. It is missing the output file option -o. The correct syntax is:
chess.o: chess.cpp
$(CC) -c chess.cpp -o chess.o $(LDFLAGS)
And finally:
chess: chess.o
$(CC) -o chess.o (LDFLAGS)
This line is missing an input argument. Make doesn't know what to compile. Also you are using the dependency file name as output argument. The filename directly after option -o specifies the output. Also a $ is missing at (LDFLAGS). The correct syntax should read:
chess: chess.o
$(CC) chess.o -o chess $(LDFLAGS)