I am trying to run a opencv c++ project from ubuntu. I ve installed properly the opencv, I ve managed to run a simple opencv cpp file. I am trying to run my MSVC++ code. I put in the same file cpp and header files. I ve created the following makefile:
CC=g++
CFLAGS = `pkg-config --cflags opencv`
LIBS = `pkg-config --libs opencv`
executable: program.o Detection.o prediction.o
$(CC) -o executable $(LIBS) program.o Detection.o prediction
program.o:
$(CC) $(CFLAGS) -c program.cpp
Detection.o:
$(CC) $(CFLAGS) -c Detection.cpp
prediction.o:
$(CC) $(CFLAGS) -c prediction.cpp
I am receiving fatal error: core.hpp: No such file or directory
compilation terminated. Any idea for what I ve got to do??
Not a solution, but several marks about your makefile:
'prediction.o' on linkage line (maybe a wrong copy-paste)
I am not sure but it is not recommanded to put space when initialize your variables CFLAGS and LIBS
It is not necessary to precise source compilation
CC=g++
CFLAGS=pkg-config --cflags opencv
LIBS=pkg-config --libs opencv
executable: program.cpp Detection.cpp prediction.cpp
$(CC) program.cpp Detection.cpp prediction.cpp -o executable $(LIBS) $(CFLAGS)
Related
I have been trying to set up my coding environment for GUI development in c++ recently, with little success. I use Manjaro Linux with Visual Studio Code, but for some reason, I always seem to get include errors when including files that I know are there.
Most recently, I tried to set up gtkmm-4.0 by installing the package and the documentation. I double checked in /usr/include/ to ensure the packages were all present, but I still am getting include errors:
cannot open source file "gtkmm.h" and
gtkmm.h:No such file or directory
At this point, all the code I have is:
#include <gtkmm.h>
#include <iostream>
int main(int argc, char* argv[]){
return 0;
}
Makefile:
exec = game.out
sources = $(wildcard src/*.cpp)
objects = $(sources:.cpp=.o)
flags = -g $(shell pkg-config gtkmm-4.0 --cflags)
libs = $(shell pkg-config gtkmm-4.0 --libs)
$(exec): $(objects)
g++ $(objects) $(flags) -o $(exec) $(libs)
%.o: %.cpp include/%.h
g++ -c $(flags) $< -o $#
install:
make
cp ./game.out /usr/local/bin/game
clean:
-rm *.out
-rm *.o
-rm src/*.o
I have scoured the internet for answers, but everything I found was either for a different os/environment or just didn't
#Galik and #John helped me solve this!
What I had to do was use g++ src/main.cpp -o main $(pkg-config gtkmm-4.0 --cflags --libs) to compile my code, then run the executable.
Thank you both for your help and guidance!!
You need to install pkg-configand add this to the compiler flags in your Makefile:
flags = -g $(shell pkg-config gtkmm-2.4 --cflags)
libs = $(shell pkg-config gtkmm-2.4 --libs)
# ...
$(exec): $(objects)
g++ $(objects) $(flags) -o $(exec) $(libs)
The tool pkg-config has a database of the correct paths for supporting libraries.
Depending on your version if gtkmm, you may need to substitute gtkmm-3.0, if you have version 3.0.
I have an app which uses Gtkmm for UI and I can compile it with a command line (saved in a script), and have read/tried to use a Makefile for it. See below:
SRCDIR = src
BINDIR = bin
OBJECTS = $(SRCDIR)/Dependency_1.o $(SRCDIR)/Dependencies_2.o $(SRCDIR)/Dependencies_N.o $(SRCDIR)/Gtkmm_Definitions.o App_Gtkmm.o
GTKFLAGS = `pkg-config --cflags gtkmm-3.0`
LIBS = `pkg-config --libs gtkmm-3.0`
CXX = g++
CXXFLAGS = -Wall -pthread -mms-bitfields
debug: EXEC = App_Gtkmm_debug
debug: $(OBJECTS)
$(CXX) $(CXXFLAGS) $(GTKFLAGS) -o $(BINDIR)/$(EXEC) $(OBJECTS) $(LIBS)
clean:
rm $(SRCDIR)/*.o $(BINDIR)/App_Gtkmm*
There is another target which I omitted for simplicity. The file structure is: Dependencies_X have class definitions which are just standard C++; Gtkmm_Definitions.hpp/.cpp have the Gtkmm-related declarations and definitions (gtkmm.h is included here) and App_Gtkmm.cpp is the main program.
When running "make debug", it will compile just part of the sources in the 1st pass and stop with some errors, that are gone when I run a 2nd time. This already seems a problem. Then it will stop when trying to compile Gtkmm_Definitions, saying it can't find gtkmm.h.
In file included from src/Gtkmm_Definitions.cpp:1:0:
src/Gtkmm_Definitions.hpp:1:10: fatal error: gtkmm.h: No such file or directory
#include <gtkmm.h>
^~~~~~~~~
compilation terminated.
make: *** [<builtin>: src/Gtkmm_Definitions.o] Error 1
However, this command line compiles without any problem:
g++ -Wall -pthread -mms-bitfields src/Dependencies_1.cpp src/Dependencies_N.cpp src/Gtkmm_Definitions.cpp App_Gtkmm.cpp -o bin/App_Gtkmm_debug `
pkg-config --cflags gtkmm-3.0 --libs gtkmm-3.0
Can you guys spot anything wrong in this Makefile? I have already tried a great deal of variables change of order, concatenation and online advices here and there.
I have installed SDL through homebrew, and it works perfectly with my test program if I enter the following command directly in the terminal:
g++ -O3 -g -Wall -Wextra -std=c++1y hello.cpp hello_main.cpp `sdl2-config --cflags --libs` -o hello
but unfortunately my attempts to write a makefile (I will definitely need one) have yielded unsuccessful/unexplained results.
I am following this, but my configuration is different/I am not specifying Cocoa (I don't need to) so I expect that the issues I am encountering are probably due in part to my different requirements:
Compiling SDL on OS X with makefile
Example:
CC=g++
CFLAGS=-c -Wall
SDLFLAGS=`sdl-config --cflags --libs` -framework Cocoa
SOURCES=main.cpp Game.cpp IO.cpp Board.cpp Pieces.cpp Piece.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=tetris
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) $(SDLFLAGS) -o $#
.cpp.o:
$(CC) $(CFLAGS) $< -o $#
clean:
rm -rf *.o $(EXECUTABLE)
My makefile so far:
CXX = g++
CXXFLAGS = -c -O3 -g -Wall -Wextra -std=c++1y
SDLFLAGS = `sdl2-config --cflags --libs`
SOURCES = hello_main.cpp hello.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECNAME = hello
all: $(SOURCES) $(EXECNAME)
$(EXECUTABLE): $(OBJECTS)
$(CXX) $ (OBJECTS) $(SDLFLAGS) -o $#
.cpp.o:
$(CXX) $(CXXFLAGS) $< -o $#
clean :
-rm -f *.o *.core $(EXECNAME)
In my .hpp header file if I #include <SDL.h> and run the one-liner command, everything is successful. If I try my makefile above, cannot be found, but if I then change the directive into #include <SDL2/SDL.h> the library is discovered. Yet the console output is the following:
g++ -c -O3 -g -Wall -Wextra -std=c++1y hello.cpp -o hello
which is odd.
Running ./hello yields a "permission denied" error, which confirms that the linking and compilation were not successful.
Everyone's system is a little bit different and the questions I've found so far don't help in this case.
I am very close to having this working (but then again, how would I start using this in an IDE? I suppose that as long as I can import the fixed makefile or build from the terminal/edit only from the IDE, I am fine.)
What changes in the makefile do I need to make?
Thank you.
EDIT:
Variation 1:
CXX = g++
CXXFLAGS = -O3 -g -Wall -Wextra -std=c++1y -c
SDLCFLAGS = `sdl2-config --cflags`
SDLLIBFLAGS = `sdl2-config --libs`
SOURCES = hello_main.cpp hello.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECNAME = hello
all: $(SOURCES) $(EXECNAME)
$(EXECUTABLE): $(OBJECTS)
$(CXX) $ (OBJECTS) $(SDLLIBFLAGS) -o $#
.cpp.o:
$(CXX) $(CXXFLAGS) $(SDLCFLAGS) $< -o $#
clean :
-rm -f *.o *.core $(EXECNAME)
I chatted with a friend and figured what was wrong: a bunch of typos and rule oddities. The following works, for anyone out there who needs a basic makefile:
CXX = g++
CXXFLAGS = -O3 -g -Wall -Wextra -std=c++1y
#LDFLAGS = -lSDL2_image
SDLCFLAGS = $(shell sdl2-config --cflags)
SDLLIBFLAGS = $(shell sdl2-config --libs)
SOURCES = hello_main.cpp hello.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECNAME = hello
all: $(EXECNAME)
$(EXECNAME): $(OBJECTS)
$(CXX) $(OBJECTS) $(SDLLIBFLAGS) $(LDFLAGS) -o $#
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) $(SDLCFLAGS) $< -o $#
clean :
-rm -f *.o *.core $(EXECNAME)
You should split your sdl2-config into two - as there are two steps. sdl2-config --cflags should go in the compiler step - thats is the .cpp:.o line in your example. The linking step should be sdl2-config --libs then. The second one seems fine for your case, the additional --cflags there does no harm but is not required.
how should properly include HIDAPI library?. After installing from the Fedora 20 repositories. It is installed in /usr/lib
This is my makefile:
CC=g++
CCFLAGS= -Wall -O0
SOURCE= Main.cpp ModuloUsb.cpp ModuloUsb.h
EXEC=modulo
test:$(SOURCE)
$(CC) $(CCFLAGS) $(SOURCE) -I../hidapi `pkg-config --libs --cflags libusb-1.0` -o $(EXEC)
After make command:
Any suggestions??
I'm trying to compile a simple program, with
#include <gtkmm.h>
The path to gtkmm.h is /usr/include/gtkmm-2.4/gtkmm.h. g++ doesn't see this file unless I specifically tell it -I /usr/include/gtkmm-2.4.
My question is, how can I have g++ automatically look recursively through all the directories in /usr/include for all the header files contained therein, and why is this not the default action?
In this case, the correct thing to do is to use pkg-config in your Makefile or buildscripts:
# Makefile
ifeq ($(shell pkg-config --modversion gtkmm-2.4),)
$(error Package gtkmm-2.4 needed to compile)
endif
CXXFLAGS += `pkg-config --cflags gtkmm-2.4`
LDLIBS += `pkg-config --libs gtkmm-2.4`
BINS = program
program_OBJS = a.o b.o c.o
all: $(BINS)
program: $(program_OBJS)
$(CXX) $(LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $#
# this part is actually optional, since it's covered by gmake's implicit rules
%.o: %.cc
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $#
If you're missing gtkmm-2.4, this will produce
$ make
Package gtkmm-2.4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtkmm-2.4.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtkmm-2.4' found
Makefile:3: *** Package gtkmm-2.4 needed to compile. Stop.
Otherwise, you'll get all the appropriate paths and libraries sucked in for you, without specifying them all by hand. (Check the output of pkg-config --cflags --libs gtkmm-2.4: that's far more than you want to type by hand, ever.)
I guess you are not using a makefile? The only thing that could be annoying is having to type the long -I option each time you compile your program. A makefile makes it a lot easier.
For example, you could modify the hello world makefile from wikipedia to something like the following:
INC=-I/usr/include/gtkmm-2.4/
helloworld: helloworld.o
g++ -o $# $<
helloworld.o: helloworld.c
g++ $(INC) -c -o $# $<
.PHONY: clean
clean:
rm -f helloworld helloworld.o
You can't. The whole point of include paths is so you can pick and choose what you want and what versions.
What you could do is..
#include <gtkmm-2.4/gtkmm.h>
Which would achieve the same effect.