Undefined reference to function in header file while trying to use cpp function in c

Viewed 27

I'm writing a project in C and would like to include a C++ library in my project without having to change all the code into C++/renaming the files. Currently I have three files, which I have dumbed down for readability here:

Voronoi.h:

#ifndef VORONOI_H
#define VORONOI_H

void calculateVoronoi(int i);

#endif /* VORONOI_H */

Voronoi.cc:

#include "Voronoi.h"

void calculateVoronoi(int i)
{
    ...;
}

Grain.c:

#include "Grain.h"
#include "Voronoi.h"

double calculateMisorientationAngle(int i)
{
    calculateVoronoi(i);
    ...;
}

I compile my program with a Makefile:

# Load the common configuration file
CC=gcc
CXX=g++

# Flags for the compilers
CFLAGS=-Wall -O3 -std=c99 -lm -g3
CXXFLAGS=-Wall -ansi -pedantic -O3 -g3 -lm
LINKERFLAGS=-Wall -ansi -O3 -g3 -lm

# define the source code and objects directory
SRC=src
OBJ=obj

# find the C source files
SRCS=$(wildcard $(SRC)/*.c)
SRCS_VORO=$(wildcard $(SRC)/*.cc)
# create object files
OBJS=$(patsubst $(SRC)/%.c,$(OBJ)/%.o, $(SRCS))
OBJS_VORO=$(patsubst $(SRC)/%.cc,$(OBJ)/%.o, $(SRCS_VORO))

# define the executable file 
BINDIR=bin
BIN=$(BINDIR)/program

$(BIN): $(OBJS) 
    $(CXX) $(E_INC) $(OBJS) -o $@  $(LINKERFLAGS) $(E_LIB)

$(OBJ)/%.o: $(SRC)/%.c
    $(CC) $(CFLAGS) -c $<  -o $@
$(OBJ)/%.o: $(SRC)/%.cc
    $(CXX) $(CXXFLAGS) $(E_INC) -c $<  -o $@ (E_LIB) -lvoro++

all: $(BIN)
    @echo Project has been compiled

However, when I try to run the makefile, I get a linker error: obj/Grain.o: in function 'calculateMisorientationAngle': C:/path/src/Grain.c:41: undefined reference to 'calculateVoronoi'

Basicly, I'm trying to use a C++ function defined in a .h header in a regular C-program. I have found countless people/issues where people try to use a C-function inside C++ needing to use the extern "C" keyword, but not the other way around. What could be the source of the error?

0 Answers
Related