alguru
alguru

Reputation: 404

"Undefined Symbols" when calling C++ function from another file

I have two sets of files: PolynomialArithmetic.h/cpp and Options.h/cpp

Options.h is defined as:

// Options.h

#ifndef Options_h
#define Options_h

#define BINARY_HEAP 0

inline int chosenHeap ();


#endif

Options.cpp is defined as:

// Options.cpp

#include "Options.h"

inline int chosenHeap() { return BINARY_HEAP; }

PolynomialArithmetic.cpp contains the following:

// PolynomialArithmetic.cpp

#include "PolynomialArithmetic.h"
#include "Options.h"

void foo () {
...
    if (chosenHeap() == BINARY_HEAP) {
        // DO SOMETHING
    }
...
}

When I compile, I get the error:

Undefined symbols for architecture x86_64:
    "chosenHeap()", referenced from: foo() in PolynomialArithmetic.o

I am guessing that this is a linking error of some sort. Here is how I compile the code:

# Makefile

# some configs
....

main: Options.o PolynomialArithmetic.o
    $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $^ -o $@ $(LDLIBS)


Options.o: Options.h Options.cpp
    $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c Options.cpp

PolynomialArithmetic.o: PolynomialArithmetic.cpp PolynomialArithmetic.h Options.h
    $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c PolynomialArithmetic.cpp 

Any ideas?

Upvotes: 0

Views: 1429

Answers (2)

Stefan
Stefan

Reputation: 1101

The inline function must be in the header, as it must be available during the compilation of PolynomialArithmetic.cpp. So the header Options.h must look like

inline int chosenHeap() { return BINARY_HEAP; }

Upvotes: 5

Kirill Kobelev
Kirill Kobelev

Reputation: 10557

Remove the inline specification from your function. Compiler is not placing any code into your Options.obj.

Upvotes: 0

Related Questions