-1

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!

Augusto Celis
  • 21
  • 1
  • 1
  • 6
  • 2
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – S.S. Anne Dec 11 '19 at 13:06
  • 3
    Two `main` functions? – Daniel Jour Dec 11 '19 at 13:06
  • Try to specify the exact error you are getting in future questions. – mfnx Dec 11 '19 at 13:09
  • 2
    Make sure you don't get totally unrelated errors in your example code before you post it. Example: `CFLAGS = -I` will make `$<` be treated as an include directory rather than a source file. – Ted Lyngmo Dec 11 '19 at 13:10

1 Answers1

1

[basic.start.main]

A program that declares a variable main at global scope, or that declares a function main at global scope attached to a named module, or that declares the name main with C language linkage (in any namespace) is ill-formed.

So, as a C++ program, it's ill-formed. Remove the C main function.

Other problems:

In the makefile you have
CFLAGS = -I
and whatever comes after that when compiling will be treated as a directory to search for header files in. In your makefile, that's the source file. Correction:
CFLAGS =
or
CFLAGS = -I.

Your header file is missing a header guard and header files that are supposed to be used by both C and C++ code usually contain the extern "C" part themselves to not burden C++ users to add it.

cpp_code.cpp

#include "header_code.h"
#include <iostream>

int main() {
    initalize(); // call the C function
    std::cout << "Hello" << std::endl;
}

c_code.c

#include "header_code.h"
#include <stdio.h>

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

header_code.h

#ifndef HEADER_CODE_H_
#define HEADER_CODE_H_

#ifdef __cplusplus
extern "C" {
#endif

extern void initalize();

#ifdef __cplusplus
}
#endif

#endif

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) $<
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Unless I am misspelling something, which I don't think I am, I am still getting an undefined reference to 'initalize' error. I do want to thank you for being so thorough with your answer though, it is greatly appreciated! – Augusto Celis Dec 11 '19 at 18:07
  • @AugustoCelis If you just copy the files from my answer (and make sure the `makefile` get characters where it should), you will not get any undefined references. You misspelled "_initialize_" but I let that be and it's spelled "_initalize_" in all 3 files - so, no undefined reference. – Ted Lyngmo Dec 11 '19 at 18:26
  • 1
    It did work. I am sorry for such a late response, but thank you very much! – Augusto Celis Jan 13 '20 at 21:31