Falak Marri
Falak Marri

Reputation: 211

Undefined reference to function errors, using C and C++ together

I have run into a problem while using C and C++ code together. The 'make' command returns "Undefined reference to function" for all functions in SPConfig.c and SPLogger.c, when called from SPImageProc.cpp

#include sections of these relevant files are given below:

SPLogger.c

#include "SPLogger.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

SPConfig.h

#ifndef SPCONFIG_H_
#define SPCONFIG_H_

#include <stdbool.h>
#include <stdio.h>
#include "SPLogger.h"
//Functions definitions
#endif /* SPCONFIG_H_ */

SPConfig.c

#include "SPConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

SPImageProc.h

#ifndef SPIMAGEPROC_H_
#define SPIMAGEPROC_H_
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <vector>

extern "C" {
#include "SPConfig.h"
#include "SPPoint.h"
}

namespace sp {
//Class and function definitions
}

SPImageProc.cpp

#include <cstdlib>
#include <cassert>
#include <cstring>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <cstdio>
#include "SPImageProc.h"
extern "C" {
#include "SPLogger.h"
}

Makefile

CC = gcc
CPP = g++
#put all your object files here
OBJS = main.o SPImageProc.o SPPoint.o 
#The executabel filename
EXEC = SPCBIR
INCLUDEPATH=/usr/local/lib/opencv-3.1.0/include/
LIBPATH=/usr/local/lib/opencv-3.1.0/lib/
LIBS=-lopencv_xfeatures2d -lopencv_features2d \
-lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_core


CPP_COMP_FLAG = -std=c++11 -Wall -Wextra \
-Werror -pedantic-errors -DNDEBUG

C_COMP_FLAG = -std=c99 -Wall -Wextra \
-Werror -pedantic-errors -DNDEBUG

.PHONY: all clean

all: $(EXEC)

$(EXEC): $(OBJS)
    $(CPP) $(OBJS) -L$(LIBPATH) $(LIBS) -o $@
main.o: main.cpp #put dependencies here!
    $(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.cpp
#a rule for building a simple c++ source file
#use g++ -MM SPImageProc.cpp to see dependencies
SPImageProc.o: SPImageProc.cpp SPImageProc.h SPConfig.h SPPoint.h SPLogger.h
    $(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.cpp
#a rule for building a simple c source file
#use "gcc -MM SPPoint.c" to see the dependencies

SPPoint.o: SPPoint.c SPPoint.h 
    $(CC) $(C_COMP_FLAG) -c $*.c
clean:
    rm -f $(OBJS) $(EXEC)

Some of Makefile errors:

SPImageProc.o: In function `sp::ImageProc::initFromConfig(sp_config_t*)':
SPImageProc.cpp:(.text+0xc8): undefined reference to `spConfigGetPCADim'
SPImageProc.cpp:(.text+0xf2): undefined reference to `spLoggerPrintError'
SPImageProc.cpp:(.text+0x12c): undefined reference to `spConfigGetNumOfImages'

I have implemented the functions in their respective C and CPP files. I have tried a lot to fix it myself and looked it up on Stack Overflow for similar problems but couldn't find a solution. Please help.

Upvotes: 2

Views: 335

Answers (1)

JeremyP
JeremyP

Reputation: 86681

You are not linking SPLogger.o and SPConfig.o

Or even compiling them for that matter.

You need to add make rules for SPLogger.o and SPConfig.o similar to SPImageProc.o and you need to add them to OBJS.

Upvotes: 1

Related Questions