salocinx
salocinx

Reputation: 3823

How to solve "Undefined reference to function" in Eclipse CDT?

I did setup a C project with Eclipse Photon (4.8.0) for developing a program for the ESP-32. I did configure the IDE according to this official setup instructions.

Flashing the ESP-32 works fine. But as soon as I try to include header files from a sub folder, I run into troubles. I have set up a very simple project to illustrate the issue. The project consists of main.c, base/test.h and base/test.c, whereas the test.h and test.c files only contain one function with the signature void function1(void);.

When I try to call function1() in main.c, I get this error in main.c:

Undefined reference to function1()

Please compare to the attached screenshot, where everything is depicted.

How to solve this issue?

enter image description here

Upvotes: 3

Views: 6976

Answers (2)

Danijel
Danijel

Reputation: 8610

Seems like you need to do proper linking.

  1. If you are linking with a library, you need to specify:
  • The name of the library: Project\Settings\C C++ General\Paths and Symbols\Libraries
  • Location where the linker should search for this library: Project\Settings\C C++ General\Paths and Symbols\Library Paths
    Important: see Note.
  1. If you are linking with object files, add those to:
  • Project\Settings\C C++ Build\Settings\Linker\Miscellaneous\Other objects

Note:

  • If your library name is, for example, libsomething.a, than you need to specify only something as the name; so omit lib prefix and .a suffix.
  • If your library is not prefixed with lib, then you need to add its name prefixed with :. For example, something.a should be added as :something.a.

Upvotes: 2

tofro
tofro

Reputation: 6073

This is not a compiler, but rather a linker error.

Note, with #includeing a header file, you only make the external function known to the compiler. You also need to link to the external function during the linking stage. Make sure you include the compiled object file that contains function1 into the link.

Upvotes: 2

Related Questions