"Undefined Reference To" Using 'G++' to Compile a C++ Program

undefined reference to using 'g++' to compile a C++ program

Use

g++ main.cpp Help.cpp

You have to tell the compiler all the files that you want it to compile, not just the first one.

Compiling C++ with C codes on VS Code using G++ compiler

The problem was solved by renaming the .C files to .CPP files it build correctly for both Debug and Release and my App was working correctly however when I ran it on another computer I got loads of missing .dlls more than I used to get from VS2019 build I think my problem is now handling how to optimize my installation of the application on any computer.

Thanks All.

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

[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) $<

Undefined reference while using g++ for compilation

Because C++ supports function overloading, the C++ compiler must ensure that the linker sees two different names for void foo(int) and void foo(float). The most common way to do that is to encode information about the parameters into the names presented to the linker.

As C does not have overloading, there is also no need for the compiler to include information about the parameters in the names presented to the linker.

If you now naively try to link a C and a C++ object file, the linker will not be able to resolve all the names, because the object files use a different naming-scheme for the names they need and/or export.

As it is quite common to use C code from a C++ program, C++ has a mechanism of telling the compiler that certain functions should use the C naming-scheme instead of the C++ scheme: You declare these functions as extern "C" int bar(), or, if you have a bunch of C functions:

extern "C" {
/* Function declarations here */
}

And where it says 'Function declarations here', you could just use #include to pull in a C header (although it is infinitely better to prepare the header itself for use in both C and C++).

To prepare a header for use in both C and C++, you first check that it does not use C++-specific keywords (such as class or new) and then you can add the following mantra to the header:

/* Include guard goes here */

#ifdef __cplusplus
extern "C" {
#endif

/* original content of the header */

#ifdef __cplusplus
}
#endif

/* #endif of the include guard here */

Cannot use g++ for compiling

Your third party C library probably lacks extern "C" declarations in its headers. To work around this without modifying the third party headers you can do something like this in your C++ source wherever you #include the relevant third party headers:

extern "C" {
#include "gaul.h" // note: I'm just guessing the names of the
#include "gaul_utils.h" // actual header files here...
}

Undefined reference to object when compiling with a header file

It's because you are not including Person.cpp as a compiling parameter. You need to add Person.cpp aswell as main.cpp file

Program compilation error undefined references using MinGW

I just tried your code with MinGW-w64 GCC 12.1.0 from https://winlibs.com/ and there was no error building with g++ main.cpp -o test.exe and the program runs fine.

So it looks like your build environment is somehow broken.

The advantage of using the standalone build from https://winlibs.com/ is that you can just extract it (no installation procedure needed), so you can leave your current build environment untouched.



Related Topics



Leave a reply



Submit