Does this makefile make sense? - c++

i want to compile this small program:
#include <iostream>
#include <SDL.h>
using namespace std;
int main() {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL init failed" << endl;
return 1;
}
cout << "Hello World" << endl;
return 0;
}
which I can do successfully with:
g++ -o SDL_basic SDL_basic.cpp -I/opt/local/include/SDL2/ -l SDL2 -L/opt/local/lib/
I spent the last hour trying to modify the makefile I usually use to do the same:
CXX=g++
INC=-I/opt/local/include/SDL2/
LIBSEARCHPATH=-L/opt/local/lib/
LIBS=-l SDL2
OBJECTS=$(PROJECT).o
PROJECT=SDL_basic
$(PROJECT) : $(OBJECTS)
${CXX} $(CPFLAGS) $(OBJECTS) -o $# ${INC} ${LIBSEARCHPATH} ${LIBS}
.cpp.o :
${CXX} -c ${CPFLAGS} $? ${INC} ${LIBSEARCHPATH} ${LIBS}
clean :
rm -f $(OBJECTS)
Running make finally works now, but if I run it a second time after a small change in the .cpp, I get the following warning:
clang: warning: -lSDL2: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-L/opt/local/lib/' [-Wunused-command-line-argument]
Therefore my question. Does this make sense what I did in the makefile? Do the arguments ${INC} ${LIBSEARCHPATH} ${LIBS} really belong after both .cpp.o : and $(PROJECT) : $(OBJECTS) (Otherwise it does not seem to work)?
I hope somebody can help me.
Thank you a lot and best regards!!
F

Related

Compilation error when including eigen library

I'm currently using eigen library, and this is the only file that I include eigen:
Kraftwerk2.cpp:
#include "Kraftwerk2.h"
Kraftwerk2::Kraftwerk2(int n){ //n: num instances
Connectivity_mat.resize(n,n);
}
int Kraftwerk2::Parse_Inst_Name(string s){ //input: String("M12"), output: int 12
return stoi(s.substr(1));
}
void Kraftwerk2::Generate_Connectivity_matrix(unordered_map<string, net> map){
for(auto& it : map){
int n =it.second.net_pin.size();
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
Connectivity_mat(i,j) = Connectivity_mat(i,j) +1;
}
}
}
}
void Kraftwerk2::Print_Mat(){
IOFormat CleanFmt(4, 0, ", ", "\n", "[", "]");
cout << Connectivity_mat.format(CleanFmt);
}
Kraftwerk2.h:
#include <iostream>
#include <Eigen/Core>
#include "module.h"
using namespace std;
// unordered_map<string, instance> instances;
// unordered_map<string, net> nets;
using namespace Eigen;
class Kraftwerk2{
public:
Kraftwerk2(int);
MatrixXd Connectivity_mat;
int Parse_Inst_Name(string);
void Generate_Connectivity_matrix(unordered_map<string, net>);
void Print_Mat();
};
And I use makefile for compiling:
(I'm pretty new to makefile, so if there's something I can improve, please tell me)
(Eigen is installed in the working directory ./eigen)
# CC and CFLAGS are varilables
CC = g++
CFLAGS = -c
OPTFLAGS = -O2
DBGFLAGS = -g -D_DEBUG_ON_
# make all
all : bin/partition
#echo -n "make complete!"
# optimized version
bin/partition: main_opt.o FM.o partition.o module.o Kraftwerk2.o
$(CC) $(OPTFLAGS) main_opt.o FM.o partition.o module.o Kraftwerk2.o -o bin/partition
main_opt.o: src/main.cpp src/FM_alg.h src/partition.h src/module.h src/Kraftwerk2.h
$(CC) -I ./eigen $< -o $#
FM.o: src/FM_alg.cpp src/FM_alg.h
$(CC) $(CFLAGS) $(OPTFLAGS) $< -o $#
partition.o: src/partition.cpp src/partition.h
$(CC) $(CFLAGS) $(OPTFLAGS) $< -o $#
Kraftwerk2.o: src/Kraftwerk2.cpp src/Kraftwerk2.h
$(CC) ./eigen $(CFLAGS) $(OPTFLAGS) $< -o $#
module.o: src/module.cpp src/module.h
$(CC) $(CFLAGS) $(OPTFLAGS) $< -o $#
# clean all the .o and executable files
clean:
rm -rf *.o lib/*.a lib/*.o bin/*
However, when I make, the terminal seems to output some error that comes form the library itself(???
g++ -I ./eigen src/main.cpp -o main_opt.o
In file included from ./eigen/Eigen/Core:269,
from src/Kraftwerk2.h:2,
from src/main.cpp:4:
./eigen/Eigen/src/Core/util/IndexedViewHelper.h:69:23: error: declaration of ‘template<class first> constexpr Eigen::Index Eigen::internal::first(const first&)’ shadows template parameter
69 | EIGEN_CONSTEXPR Index first(const T& x) EIGEN_NOEXCEPT { return x.first(); }
| ^~~~~
./eigen/Eigen/src/Core/util/IndexedViewHelper.h:68:10: note: template parameter ‘first’ declared here
68 | template<typename T>
| ^~~~~~~~
make: *** [makefile:15: main_opt.o] Error 1
I would be so appreciated if anybody can tell me what's wrong with this.

Can't compile simple SDL program with Dev-C++

Just before you read just know that i'm not english, so hopefully i won't misspell my writing here.
Anyway. I was trying to compile my first SDL program, so i followed online tutorials to install SDL2 libraries. The code (copied from here min 13:00) i used is this :
#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
int main(void){
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL init failed.\n";
return 1;
}
cout << "SDL init succeeded";
SDL_Quit();
return 0;
}
The error i get is this
C:\Users\raffaele.ciotola\Desktop\Marco & Lory\Lorenzo\Dev-Cpp\SDL2-2.0.12\x86_64-w64-mingw32\lib\libSDL2main.a(SDL_windows_main.o) In function `main_getcmdline':
71 s:\rs\valve\release\SDL\SDL2-2.0.12-source\src\main\windows\SDL_windows_main.c undefined reference to `SDL_main'
C:\Users\raffaele.ciotola\Desktop\Marco & Lory\Lorenzo\Dev-Cpp\Programs\SDL_\collect2.exe [Error] ld returned 1 exit status
25 C:\Users\raffaele.ciotola\Desktop\Marco & Lory\Lorenzo\Dev-Cpp\Programs\SDL_\Makefile.win recipe for target 'SDL_.exe' failed
I tried running my Dev-Cpp.exe on administrator, since the installation folder is on the desktop, but that didn't solve the problem.
The Makefile (Whatevere it is, i don't have the minimal idea) is this. If needed ¯_(ツ)_/¯.
# Project: Progetto3
# Makefile created by Dev-C++ 5.11
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = SDL_.o
LINKOBJ = SDL_.o
LIBS = -L"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/lib" -L"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -L"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/SDL2-2.0.12/x86_64-w64-mingw32/lib" -static-libgcc -mwindows -lmingw32 -lSDL2main -lSDL2 -lopengl32 -lglu32
INCS = -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
CXXINCS = -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/SDL2-2.0.12/x86_64-w64-mingw32/include"
BIN = SDL_.exe
CXXFLAGS = $(CXXINCS)
CFLAGS = $(INCS)
RM = rm.exe -f
.PHONY: all all-before all-after clean clean-custom
all: all-before $(BIN) all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
SDL_.o: SDL_.cpp
$(CPP) -c SDL_.cpp -o SDL_.o $(CXXFLAGS)
If you need any other info just ask. Thank You.
SDL hijacks the main function with its own in order to do some initial setup. It then calls whatever you have written as the main function. Because it is calling your main function it expects it to be defined in a specific way.
Try this and it should resolve the errors you are encountering:
int main(int argc, char* args[])
{
// whatever
return 0;
}

The executable test file is not running the tests

I have created a project:
// func.h :
#ifndef FUNCS_H_
#define FUNCS_H_
int addInts(int i, int j);
#endif /* FUNCS_H_ */
// func.cpp
#include "func.h"
int addInts(int i, int j) {
return i + j;
}
// main.cpp
#include "func.h"
#include <iostream>
int main() {
std::cout << addInts(5, 7) << std::endl;
return 0;
}
//makefile
OBJS := \
main.o \
func.o
CXX := g++
CXX_FLAGS := -Wall -Werror -Wextra -std=c++11
all: main_prj
main_prj: $(OBJS)
$(CXX) $(OBJS) -lm -o main
-include $(OBJS:.o=.d)
%.o: %.cpp
$(CXX) -c $(CXX_FLAGS) $*.cpp -o $*.o
clean:
rm -f main $(OBJS)
And I created also a test (boost test) for that function:
// test/main_test.cpp
#define BOOST_TEST_MODULE main_test
#include <boost/test/included/unit_test.hpp>
//____________________________________________________________________________//
// FIXTURES ...
//____________________________________________________________________________//
// test/addInts/addInts_test.cpp
#include <boost/test/unit_test.hpp>
#include "../../func.h"
BOOST_AUTO_TEST_CASE(addInts_5_7) {
int addInts_5_7 = 5 + 7;
BOOST_CHECK_EQUAL(addInts_5_7, addInts(5, 7));
}
// test/makefile
OBJS_TEST := \
main_test.o \
addInts/addInts_test.o \
../func.o
CXX_TEST := g++
CXX_FLAGS_TEST := -Wall -Werror -Wextra -std=c++11
all_test: main_test_prj
main_test_prj: $(OBJS_TEST)
$(CXX_TEST) $(OBJS_TEST) -lm -o main_test
-include $(OBJS_TEST:.o=.d)
%.o: %.cpp
$(CXX_TEST) -c $(CXX_FLAGS_TEST) $*.cpp -o $*.o
clean_test:
rm -f main_test $(OBJS_TEST)
Now, the make commands work, so they clean all the created files, or create the o files and the executables. When I run the main file, the output is correct: 12 ; but when I run the main_test file, the output is a little strange:
Running 1 test case...
*** No errors detected
I would expect the output with running tests and OK/KO, or pass/not pass... I cannot figure what am I doing wrong. Can anyone help me, please?
You have one test.
The output says that one test was run, and that no errors were found.
This output appears to be as documented.
There is no problem here.
Though, sadly, the documentation on how to change this output is rather lacking…

missing template arguments before [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have problem in compelling my code. The error message indicates "missing template arguments" in Map.h. But I do believe there is not any error in Map.h, because it is from Minisat, which is a sophistic API. So I think it is my fault in code or in makefile. I have tried so many times, can you help me on this? Thank you so much!
The error message is:
Compiling: queryOrac/queryOrac.o
In file included from /home/parallels/Desktop/Incremental_Solver/core/SolverTypes.h:30:0,
from /home/parallels/Desktop/Incremental_Solver/core/Solver.h:28,
from /home/parallels/Desktop/Incremental_Solver/simp/SimpSolver.h:25,
from /home/parallels/Desktop/Incremental_Solver/queryOrac/queryOrac.cc:12:
/home/parallels/Desktop/Incremental_Solver/mtl/Map.h: In member function ‘uint32_t Minisat::Hash<K>::operator()(const K&) const’:
/home/parallels/Desktop/Incremental_Solver/mtl/Map.h:32:99: error: missing template arguments before ‘(’ token
template<class K> struct Hash { uint32_t operator()(const K& k) const { return hash(k); } };
^
/home/parallels/Desktop/Incremental_Solver/mtl/Map.h: In member function ‘uint32_t Minisat::DeepHash<K>::operator()(const K*) const’:
/home/parallels/Desktop/Incremental_Solver/mtl/Map.h:35:103: error: missing template arguments before ‘(’ token
template<class K> struct DeepHash { uint32_t operator()(const K* k) const { return hash(*k); } };
^
make: *** [/home/parallels/Desktop/Incremental_Solver/queryOrac/queryOrac.o] Error 1
The .cc file is (This file is a implementation of a .h file):
#include <map>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <iostream>
#include <regex>
#include "queryOrac/queryOrac.h"
#include "incre/tools.h"
#include "incre/dict.h"
#include "simp/SimpSolver.h"
#include "utils/System.h"
#include "utils/ParseUtils.h"
#include "utils/Options.h"
#include "core/Dimacs.h"
using namespace std;
using namespace Minisat;
using namespace Incre;
Oracle::Oracle(const char * ora, const char * PI, const char * PO)
{
cout << "a Oracle is created" << endl;
PI_path = PI;
PO_path = PO;
Orac_Path = ora;
}
void Oracle::process()
{
parse_PI();
assign_PI();
solve();
}
void Oracle::parse_PI() {
cout << "reading from " << PI_path << endl;
ifstream infile;
infile.open(PI_path, ios::in);
string first_line;
string second_line;
getline(infile, first_line);
getline(infile, second_line);
vector<string> name_temp;
vector<string> value_temp;
SplitString(first_line, name_temp, " ");
SplitString(second_line, value_temp, " ");
vector<string>::iterator value = value_temp.begin();
for(vector<string>::iterator name = name_temp.begin(); name != name_temp.end(); ++name)
{
PI_temp.insert(pair<int, string>(varIndexDict[*name], *value));
value++;
}
}
void Oracle::assign_PI(){
vector<int>::iterator position = pisIndex.begin();
for(map<int, string>::iterator index = PI_temp.begin(); index != PI_temp.end(); ++index)
{
if(index->second == "1") PI_assignment_cnf.push_back(tostring(*position) + " 0\n");
else if(index->second == "0") PI_assignment_cnf.push_back("-" + tostring(*position) + " 0\n");
position++;
}
PI_assignment_cnf.insert(PI_assignment_cnf.begin(), "c this is assign_PI\n");
cnFile += PI_assignment_cnf;
print_vector(cnFile, "cnFile");
}
void Oracle::solve(){
cout << " start solving" << endl;
}
my makefile is:
##
## Template makefile for Standard, Profile, Debug, Release, and Release-static versions
##
## eg: "make rs" for a statically linked release version.
## "make d" for a debug version (no optimizations).
## "make" for the standard version (optimized, but with debug information and assertions active)
PWD = $(shell pwd)
EXEC ?= $(notdir $(PWD))
CSRCS = $(wildcard $(PWD)/*.cc)
DSRCS = $(foreach dir, $(DEPDIR), $(filter-out $(MROOT)/$(dir)/Main.cc, $(wildcard $(MROOT)/$(dir)/*.cc)))
CHDRS = $(wildcard $(PWD)/*.h)
COBJS = $(CSRCS:.cc=.o) $(DSRCS:.cc=.o)
PCOBJS = $(addsuffix p, $(COBJS))
DCOBJS = $(addsuffix d, $(COBJS))
RCOBJS = $(addsuffix r, $(COBJS))
CXX ?= g++
CFLAGS ?= -Wall -Wno-parentheses -std=c++11
LFLAGS ?= -Wall
COPTIMIZE ?= -O3
CFLAGS += -I$(MROOT) -D __STDC_LIMIT_MACROS -D __STDC_FORMAT_MACROS
LFLAGS += -lz
.PHONY : s p d r rs clean
s: $(EXEC)
p: $(EXEC)_profile
d: $(EXEC)_debug
r: $(EXEC)_release
rs: $(EXEC)_static
libs: lib$(LIB)_standard.a
libp: lib$(LIB)_profile.a
libd: lib$(LIB)_debug.a
libr: lib$(LIB)_release.a
## Compile options
%.o: CFLAGS +=$(COPTIMIZE) -g -D DEBUG
%.op: CFLAGS +=$(COPTIMIZE) -pg -g -D NDEBUG
%.od: CFLAGS +=-O0 -g -D DEBUG
%.or: CFLAGS +=$(COPTIMIZE) -g -D NDEBUG
## Link options
$(EXEC): LFLAGS += -g
$(EXEC)_profile: LFLAGS += -g -pg
$(EXEC)_debug: LFLAGS += -g
#$(EXEC)_release: LFLAGS += ...
$(EXEC)_static: LFLAGS += --static
## Dependencies
$(EXEC): $(COBJS)
$(EXEC)_profile: $(PCOBJS)
$(EXEC)_debug: $(DCOBJS)
$(EXEC)_release: $(RCOBJS)
$(EXEC)_static: $(RCOBJS)
lib$(LIB)_standard.a: $(filter-out */Main.o, $(COBJS))
lib$(LIB)_profile.a: $(filter-out */Main.op, $(PCOBJS))
lib$(LIB)_debug.a: $(filter-out */Main.od, $(DCOBJS))
lib$(LIB)_release.a: $(filter-out */Main.or, $(RCOBJS))
## Build rule
%.o %.op %.od %.or: %.cc
#echo Compiling: $(subst $(MROOT)/,,$#)
#$(CXX) $(CFLAGS) -c -o $# $<
## Linking rules (standard/profile/debug/release)
$(EXEC) $(EXEC)_profile $(EXEC)_debug $(EXEC)_release $(EXEC)_static:
#echo Linking: "$# ( $(foreach f,$^,$(subst $(MROOT)/,,$f)) )"
#$(CXX) $^ $(LFLAGS) -o $#
## Library rules (standard/profile/debug/release)
lib$(LIB)_standard.a lib$(LIB)_profile.a lib$(LIB)_release.a lib$(LIB)_debug.a:
#echo Making library: "$# ( $(foreach f,$^,$(subst $(MROOT)/,,$f)) )"
#$(AR) -rcsv $# $^
## Library Soft Link rule:
libs libp libd libr:
#echo "Making Soft Link: $^ -> lib$(LIB).a"
#ln -sf $^ lib$(LIB).a
## Clean rule
clean:
#rm -f $(EXEC) $(EXEC)_profile $(EXEC)_debug $(EXEC)_release $(EXEC)_static \
$(COBJS) $(PCOBJS) $(DCOBJS) $(RCOBJS) *.core depend.mk
## Make dependencies
depend.mk: $(CSRCS) $(CHDRS)
#echo Making dependencies
#$(CXX) $(CFLAGS) -I$(MROOT) \
$(CSRCS) -MM | sed 's|\(.*\):|$(PWD)/\1 $(PWD)/\1r $(PWD)/\1d $(PWD)/\1p:|' > depend.mk
#for dir in $(DEPDIR); do \
if [ -r $(MROOT)/$${dir}/depend.mk ]; then \
echo Depends on: $${dir}; \
cat $(MROOT)/$${dir}/depend.mk >> depend.mk; \
fi; \
done
-include $(MROOT)/mtl/config.mk
-include depend.mk
Your problem is due to using namespace std;. Don't do that, ever.
Some people say it's fine as long as it's not in a header file, but they are wrong, and your error is a great example.
In this case, you're accidentally referring to std::hash, which is a class template, so the <> cannot be omitted, unlike for function templates.

MongoDB example failure to compile with different flags

So I had a really strange compiler result after compiling the snippet provided by the MongoDB Quickstart (New Driver).
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
mongocxx::instance inst{};
mongocxx::client conn{};
bsoncxx::builder::stream::document document{};
auto collection = conn["testdb"]["testcollection"];
document << "hello" << "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
The code compiles perfectly with
c++ --std=c++11 hellomongo.cpp -o hellomongo $(pkg-config --cflags --libs libmongocxx)
but failed to compile when I made a makefile and added some flags
CXX=g++
FLAGS=-v -Wall --std=c++11 -I/usr/local/include
EXENAME=matchingengine
SOURCES=main.cpp
OBJECTS=main.o
all: $(EXENAME)
$(EXENAME): $(OBJECTS)
$(CXX) $(FLAGS) $(SOURCES) -o $(EXENAME)
clean:
rm *o $(EXENAME)
I keep getting
main.cpp:3:10: fatal error: 'bsoncxx/builder/stream/document.hpp' file not found
Where i have adjusted the -I include directory pointing to the place where bsoncxx is located. Please help me on this compiler issue.
I solved it
Makefile
CXX=g++
CFLAGS=-c --std=c++11 -Wall -I/usr/local/include/mongocxx/v0.3 -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/bsoncxx/v0.3 -I/usr/local/include/libbson-1.0
LDFLAGS=-L/usr/local/lib -lmongocxx -lbsoncxx
SOURCES=main.cpp
OBJECTS=main.o
EXECUTABLE=matchingengine
all: $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CXX) $(OBJECTS) -o $(EXECUTABLE) $(LDFLAGS)
$(OBJECTS): $(SOURCES)
$(CXX) $(CFLAGS) -c $(SOURCES)