fiz
fiz

Reputation: 399

Linker Error in C, Xcode. Linking object files (.o) fails. Not sure how to include makefile in Xcode

I have created Xcode project and added C files and headers that I want to use. However, I can't figure out how to link object files and that's why I get Linker Error.

tree of my files and linking error

Speller.c has main method. And now I am trying to link dict-tree.o and speller.o from header file dict.h .

I can do it from terminal using makefile that I wrote, but I need a proper debugging and that is why decided to use Xcode.

Is there a way to include makefile, and compile program through that makefile on Xcode?

Makefile content:

  CFLAGS=-g -Wall -std=c99
  CC=gcc

  # "make tree" or "make hash" to compile

  tree: speller.o dict-tree.o
       $(CC) $(LDFLAGS) -o $@ $^

  hash: speller.o dict-hash.o
       $(CC) $(LDFLAGS) -o $@ $^

Upvotes: 1

Views: 1471

Answers (1)

Mike
Mike

Reputation: 11

After having this same problem, finding this question and then some further poking around, I discovered you need to declare the C-based functions in your C++ file as such:

extern "C" void function_from_c_file(int var, int var);

This has to take the place of the standard function declaration in the header file associated with the C file.

Basically you need a separate header file for use in your C++ files, or hand code them into your C++ files as needed.

I found help in these two links:

https://discussions.apple.com/thread/2197357?tstart=0 (go down to the post by "etresoft")

https://isocpp.org/wiki/faq/mixing-c-and-cpp#call-c

Good luck!

Upvotes: 1

Related Questions