Undefined referece to class constructor - c++

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 $# $<

Related

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'.

Why is the makefile returning g++: error: h file or directory make: *** [Makefile:12: test] Error 1?

I want to compile this program test.cpp
#include <iostream>
#include <cstring>
#include <iomanip>
#include <sodium.h>
#include <gmpxx.h>
#include <sstream>
using namespace std;
//Encryt or decrypt with RSA
mpz_class RSA(mpz_class m, mpz_class e,mpz_class N)
{
mpz_class rop;
mpz_powm(rop.get_mpz_t(), m.get_mpz_t(), e.get_mpz_t(), N.get_mpz_t());
return rop;
}
int main(const int argc, const char *const argv[])
{
if (argc!=4){
printf("usage: %s [Message] [Exponent] [Modulus] \n", argv[0]);
return 1;
}
const mpz_class m(argv[1]), d(argv[2]),N(argv[3]);
cout<<endl<<RSA(m,d,N);
return 0;
}
with this makefile Makefile
CXX = g++
CXXFLAGS = -c -Wall -Wextra -Werror
LDFLAGS = -lgmp -lsodium -lssl -lcrypto -lgmpxx
SRC = $(wildcard *.cpp )
HDR = $(wildcard *.h )
OBJ = $(SRC :.cpp =.o )
all : Release
Debug : CXXFLAGS +=-g
Debug : test
Release : test
test : $(OBJ)
$(CXX) -o $# $ˆ $(LDFLAGS)
%.o : %.cpp $(HDR)
$(CXX) $(CXXFLAGS) $< -o $#
clean :
rm -f $(OBJ) test
But I receive
-------------- Build: Debug in KA-RMP (compiler: GNU GCC Compiler)---------------
Checking if target is up-to-date: make -q -f Makefile Debug
Running command: make -f Makefile Debug
g++: error: h file or directory
make: *** [Makefile:12: test] Error 1
g++ -o test lsodium -lssl -lcrypto -lgmpxx
Process terminated with status 2 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))
Do you have any idea why I receive the error?
The problem is that $^ and $# is not working as you expected.
$# and $^ return the left and right values of $OBJ. However $OBJ simply resolves to "test.o".
OBJ = $(SRC:.cpp=.o) # converts test.cpp to test.o
Therefore $^ will return nothing because there is nothing on the right side.
If you want to use $^ you have to set OBJ to "test.o test.cpp". I made the changes with your makefile.
Otherwise use $(CXX) -o $# $(SRC) $(LDFLAGS).
CXX = g++
CXXFLAGS = -c -Wall -Wextra -Werror
LDFLAGS = -lgmp -lsodium -lssl -lcrypto -lgmpxx
SRC = $(wildcard *.cpp )
HDR = $(wildcard *.h )
OBJ = $(SRC) $(SRC :.cpp =.o )
all : Release
Debug : CXXFLAGS +=-g
Debug : test
Release : test
test : $(OBJ)
$(CXX) -o $# $^ $(LDFLAGS)
%.o : %.cpp $(HDR)
$(CXX) $(CXXFLAGS) $< -o $#
clean :
rm -f $(OBJ) test

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

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)'

avr32-gcc: Global variable not initialized with function output

I try to initialize a global variable with the output of a function. It works as expected for gnu-gcc, but when compiled with avr32-gcc, the variable is initialized with 0. I want the variable to be initialized by a function since in the real scenario it is a reference (MyClass& myclass = MyClass::getInstance().
Here is the code:
extern "C"
{
#include "led.h"
}
int getInteger()
{
return 10;
}
int my_int = getInteger();
int main (void)
{
LED_On(LED2);
//my_int = getInteger(); //* This line really sets my_int = 10;
if(my_int == 10)
{
LED_On(LED1);
}
while(1){
__asm__ ("nop");
}
return 0;
}
and here is my Makefile:
PROJ_NAME=TEST
# Include paths for project folder
SRCS_INC += -I. -I./preprocessor
# Sources files for project folder (*.c and *.cpp)
SRCS += main.cpp exception_noNMI.S startup_uc3.S trampoline_uc3.S intc.c led.c
AS = avr32-gcc
ASFLAGS = -x assembler-with-cpp -c -mpart=uc3c1512c -mrelax
ASFLAGS += ${SRCS_INC}
CC = avr32-gcc
CFLAGS += -mpart=uc3c1512c
CFLAGS += ${SRCS_INC}
CXX = avr32-g++
LINKER = avr32-g++
OBJCOPY = avr32-objcopy
LDFLAGS = -nostartfiles -mpart=uc3c1512c
LDFLAGS += ${SRCS_INC}
OBJS += $(addsuffix .o, $(basename $(SRCS)))
# Main rule
all: $(PROJ_NAME).elf
# Linking
${PROJ_NAME}.elf: ${OBJS}
#echo Linking...
#$(CXX) $(LDFLAGS) $^ -o $#
#$(OBJCOPY) -O ihex ${OBJCOPYFLAGS} $(PROJ_NAME).elf $(PROJ_NAME).hex
#$(OBJCOPY) -O binary $(PROJ_NAME).elf $(PROJ_NAME).bin
# Assembly files in source folder
%.o: ./%.S
#mkdir -p $(dir $#)
#$(AS) $(ASFLAGS) -c $< -o $#
# C files in source folder
%.o: ./%.c
#mkdir -p $(dir $#)
#$(CC) -c $< -o $# $(CFLAGS)
# CPP files in SRC folder
%.o: ./%.cpp
#mkdir -p $(dir $#)
#$(CXX) -c $< -o $# $(CFLAGS)
.PHONY: clean
clean:
#rm -f $(OBJS) $(PROJ_NAME).elf $(PROJ_NAME).hex $(PROJ_NAME).bin
I also tried to make an init function with __attribute(constructor) as explained in this answer.
Still it is not excuted.
As far as I can see, you're not having optimization enabled. Thus, the compiler may actually make a call to getInteger during startup. Now, you're replacing the default startup with something that you do not show. Maybe you just need to make sure that this is done during startup? Also, you could try to enable optimization and see if the call is resolved.

c++ undefined reference to destructor

Sorry about this, but I am re-opening this. After sorting the eigen errors, this cropped right back up again. Exactly the same code exactly the same error. (well, the compiler found the eigen headers this time.) So, same question:
I have searched for destructor c++ and undefined reference to no avail. However I am pretty sure this is a fairly simple slip on my part.
Error:
/tmp/ccDsaJ9v.o: In function `main':
geomSetup.cpp:(.text+0x5ab): undefined reference to `SASAGeometry::~SASAGeometry()'
geomSetup.cpp:(.text+0x5cd): undefined reference to `SASAGeometry::~SASAGeometry()'
collect2: ld returned 1 exit status
make: *** [geomTest] Error 1
SASAGeometry.h:
class SASAGeometry
{
public:
//methods
SASAGeometry() ;
int makeFromFiles(char *, char *, char *) ;
~SASAGeometry() ;
//globals
std::list<E......};
SASAGeometry.cpp
SASAGeometry::SASAGeometry(){}
int SASAGeometry::makeFromFiles(char * xyz_file, char * dat_file, char * atoms_file)
{
sasa_transformMatrix basisMaker ;
list<Vect...
...
}
SASAGeometry::~SASAGeometry(){}
geomTest.cpp
int main(int argv, char * argc[])
{
list<Vector3d>::iterator listIterator ;
char * xyz_file = argc[1] ;
char * dat_file = argc[2] ;
char * atoms_file = argc[3] ;
SASAGeometry geomMaker ;
int geomErr....
...
return 0 ;
}
makefile :
# compiler choice
CXX = g++
# executable path
BIN = .
# include paths (or lack thereof :p)
INCLUDE = -I .
# compilation flags
CXXFLAGS = -pipe # -O6
# linking flags
LFLAGS = -lm
# object declarations
GeomTest_OBJS = geomTest.o SASAGeometry.o
geomTest_source = SASAGeometry.cpp SASAGeometry.h sasa_transformMatrix.cpp sasa_transformMatrix.h geomSetup.cpp
SASAGeometry.o : SASAGeometry.cpp SASAGeometry.h sasa_transformMatrix.cpp sasa_transformMatrix.h
geomTest.o : geomSetup.cpp SASAGeometry.o
# compile
geomTest : $(GeomTest_OBJS) makefile
$(CXX) -o geomTest.o -o SASAGeometry.o $(LIBS) $(INCLUDE) $(CXXFLAGS) $(geomTest_source) $(LFLAGS)
$(CXX) $(LIBS) $(INCLUDE) $(CXXFLAGS) -o $(BIN)/geomTest geomTest.o SASAGeometry.o $(LFLAGS)
clean : \rm *.o *~ p1
My INCLUDE and LIBS flags are all ok, all other methods in the SASAGeometry class are quite happily defined.
Thanks in advance.
You are not including SASAGeometry.cpp in makefile along with geomTest.cpp. makefile should be something like this:
geomTest : $(GeomTest_OBJS) makefile
$(CC) -o geomTest.o -o SASAGeometry.o $(LIBS) $(INCLUDE) $(CFLAGS) $(geomTest_source) $(LFLAGS)
$(CC) $(LIBS) $(INCLUDE) $(CFLAGS) -o $(BIN)/geomTest geomTest.o $(LFLAGS)