undefined reference to Error when making c++ file including opencv - c++

I'm trying to make a cpp file that simply displays an image :
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main(){
Mat img;
img = imread("img.jpg",1);
if (!img.data){
std::cout << "Could not open or find the image" << std::endl;
return -1;
}
namedWindow("my window", 1);
int key = 0;
while (key != 27){
imshow("my window", img);
waitKey(10);
}
destroyAllWindows();
return 0;
}
and I am using this Makefile:
CC=g++
CFLAGS=-W -Wall -ansi -pedantic -I/usr/include/opencv2
LDFLAGS=-lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_objdetect
EXEC=DetectRT
SRC= $(wildcard *.cpp)
OBJ= $(SRC:.cpp=.o)
all: $(EXEC)
$(EXEC): $(OBJ)
$(CC) -o $# $^ $(LDFLAGS)
%.o: %.cpp
$(CC) -o $# -c $< $(CFLAGS)
.PHONY: clean mrproper
clean:
rm -rf *.o
mrproper: clean
rm -rf $(EXEC)
However, when I'm making I get errors like :
Test.o: In function `main':
Test.cpp:(.text+0x46): undefined reference to `cv::imread(cv::String const&, int)'

Related

Using makefile to link cimgui to project?

I have a c project and I'm trying to link the C binding for imgui to it using Makefiles but I'm having some trouble. I've already generated the .dll using cmake in a build folder, these are the contents of it:
C:\path\to\cimgui\build:
-CMakefiles
-cimgui.dll
-cmake_install.txt
-CMakeCache.txt
-libcimgui.dll.a
-Makefile
and this is my makefile
PROJ := proj
TARGET := $(PROJ)
OBJ := sub.o main.o
INCLUDE_PATH := -IC:\path\to\SDL2 -IC:\path\to\cimgui
LIB_PATH := -LC:\path\to\SDL2\lib -LC:\path\to\cimgui\build
LINKER_FLAGS := -lSDL2 -lopengl32 -lcimgui -lcimgui.dll
CFLAGS := $(INCLUDE_PATH)
.PHONY : build clean
build : $(TARGET).exe
$(TARGET).exe: $(OBJ)
gcc $^ $(LIB_PATH) $(LINKER_FLAGS) -o $#
sub.o: sub.c sub.h
gcc -c $(CFLAGS) $< -o $#
main.o: main.c sub.h
$(CC) $(CFLAGS) $< -o $#
clean :
#rm -fv *.o
#rm -fv *.exe
and this is my main.c project
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include "cimgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_opengl.h>
int main(int argc, char ** argv)
{
return 0;
}
This is the error I get when i try to compile my makefile:
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lcimgui.dll

Dev-C++ 5.11 with MinGW have linking problem

I did try to compile a OpenCV-Project with MinGW (under Windows 10). The OpenCV-library had compiled without problem and I try to do couple of test.
main3.cpp
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat im = imread(argc == 2 ? argv[1] : "lena.jpg", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
I have follows Makefile:
# Project: Proj400-v3
# Makefile created by Dev-C++ 5.11
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = obj/main3.o
LINKOBJ = obj/main3.o
LIBS = -L"C:/RD/TMP/Dev-Cpp/MinGW64/lib32" -L"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/lib" -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400 -m32
INCS = -I"C:/RD/TMP/Dev-Cpp/MinGW64/include" -I"C:/RD/TMP/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/RD/TMP/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/inc" -I"C:/RD/TMP/Work/entw/OCV-SVD/proj4.0.0.0"
CXXINCS = -I"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/inc" -I"C:/RD/TMP/Work/entw/OCV-SVD/proj4.0.0.0"
BIN = obj/Proj400-v3.exe
CXXFLAGS = $(CXXINCS) -m32 -std=c++11 -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400
CFLAGS = $(INCS) -m32
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)
obj/main3.o: main3.cpp
$(CPP) -c main3.cpp -o obj/main3.o $(CXXFLAGS)
All compiled and linked without mistakes nad run fine. I try to expand the test with new file 'second3.cpp'.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h>
extern "C" {
}
New the Makefile:
# Project: Proj400-v3
# Makefile created by Dev-C++ 5.11
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = obj/main3.o obj/second3.o
LINKOBJ = obj/main3.o obj/second3.o
LIBS = -L"C:/RD/TMP/Dev-Cpp/MinGW64/lib32" -L"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/lib" -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400 -m32
INCS = -I"C:/RD/TMP/Dev-Cpp/MinGW64/include" -I"C:/RD/TMP/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/RD/TMP/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/inc" -I"C:/RD/TMP/Work/entw/OCV-SVD/proj4.0.0.0"
CXXINCS = -I"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/inc" -I"C:/RD/TMP/Work/entw/OCV-SVD/proj4.0.0.0"
BIN = obj/Proj400-v3.exe
CXXFLAGS = $(CXXINCS) -m32 -std=c++11 -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400
CFLAGS = $(INCS) -m32
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)
obj/main3.o: main3.cpp
$(CPP) -c main3.cpp -o obj/main3.o $(CXXFLAGS)
obj/second3.o: second3.cpp
$(CPP) -c second3.cpp -o obj/second3.o $(CXXFLAGS)
After compiling I have follows list of mistakes:
- Command: mingw32-make.exe -f "C:\RD\TMP\Work\entw\OCV-SVD\proj4.0.0.0\Makefile.win" all
g++.exe obj/main3.o obj/second3.o -o obj/Proj400-v3.exe -L"C:/RD/TMP/Dev-Cpp/MinGW64/lib32" -L"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/lib" -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400 -m32
obj/main3.o:main3.cpp:(.text+0xc3): undefined reference to `cv::imread(std::string const&, int)'
obj/main3.o:main3.cpp:(.text+0x181): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
collect2.exe: error: ld returned 1 exit status
C:\RD\TMP\Work\entw\OCV-SVD\proj4.0.0.0\Makefile.win:25: recipe for target 'obj/Proj400-v3.exe' failed
mingw32-make.exe: *** [obj/Proj400-v3.exe] Error 1
Can some body explane me, what is wrong? Why with one source in the project it work fine und with many files in the project I have 'undefined reference'?
PS: If I change the project from two source-files to one-source, but do not remove the obj/second3.o, I have again problem with 'undefined reference'.

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;
}

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)

Undefined referece to class constructor

I'm getting the following error:
> /tmp/ccYbdvB8.o: In function `main':
> /home/caleb/Documents/dev/cs438/tote2/mp2/manager.cpp:5: undefined
> reference to `TCPConnection::TCPConnection(char const*, char const*)'
> collect2: error: ld returned 1 exit status make: *** [manager] Error 1
I've included the header to the class I'm trying to initialize.. so I'm thinking maybe it's my makefile? I'm pretty new to writing custom makefiles...
cpp:
#include "tcpcon.h"
const string TCPConnection::Client = "client";
const string TCPConnection::Server = "server";
TCPConnection::TCPConnection(const char* t, const char* p) : target(t), port(p)
{ }
h:
class TCPConnection{
public:
TCPConnection(const char *target, const char *port);
main:
#include "tcpcon.h"
int main()
{
TCPConnection *TCPCon = new TCPConnection("localhost", "7777");
cout << "Hi\n";
return 0;
}
makefile:
CC=g++
CCOPTS=-Wall -Wextra -g
OBJS = tcpcon.o
TARGETS = manager
.PHONY: all clean
$(TARGET) : $(OBJS)
$(CC) -o $# $^ $(CFLAGS) $(LIBS)
all: $(OBJS) $(TARGETS)
clean:
rm -f $(TARGETS) $(OBJS)
%: %.cpp
$(CC) $(CCOPTS) -o $# $<
It turns out that the issue may have been related to a couple of things:
Use of $(TARGET) instead of $(TARGETS) in one place
Did not include the main file (manager.cpp) to the OBJS list
My updated makefile is below:
CC=g++
CCOPTS=-Wall -Wextra -g
OBJS = manager.o tcpcon.o
TARGETS = manager
.PHONY: all clean
$(TARGETS) : $(OBJS)
$(CC) -o $# $^ $(CFLAGS) $(LIBS)
all: $(TARGETS) $(OBJS)
clean:
rm -f $(TARGETS) $(OBJS)
%: %.cpp
$(CC) $(CCOPTS) -o $# $<