mercredi 11 décembre 2019

Undefined reference when compiling a C and C++ file with same header

To make this short, I have a CPP and C code, and my CPP code is trying to reference functions from the C code with a header file. Whenever I run the make command, I end up getting "undefined reference" errors. Here are my codes:

cpp_code.cpp:

extern "C"{
    #include "header_code.h";
}

int main(){
    cout << "Hello" << endl;
    return 0;
}

c_code.c:

#include "header_code.h"

int main(){
    printf("Hello");
    return 0;
}

void initalize(){
    printf("Initilized");
}

header_code.h:

extern void initalize();

makefile:

CXX = g++
CXXFLAGS = -std=c++11
CC = gcc
DEPS = header_code.h
CFLAGS = -I
OBJS = cpp_code.o c_code.o

c: $(OBJS) 
    $(CXX) -o $@ $^ $(CXXFLAGS)

%.o : %.cpp
    $(CXX) -c $(CXXFLAGS) $<

%.o : %.c $(DEPS)
    $(CC) -c $(CFLAGS) $<

Whenever running make it always gives me problems. Can anyone please help me? Thank you for your time reading all of this!

Aucun commentaire:

Enregistrer un commentaire