1
votes

I would like to build c project by Eclipse. The codes are ok (like following) but link is not worked. So I cannot build them.If the contents of sub() is defined in sub.h, build works fine.

I often use Microsoft visual studio, but I am a begginer of eclipse and I could not set the linker option.

What I want to do

How can I link and build devided c-sources by eclipse? I want to build the project by just clicking "Build All" button.

Error message

  • undefined reference to `sub'

  • collect2: error: ld returned 1 exit status

Eclipse setting

enter image description here

main.c

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

int main(void)
{
  printf("sub() = %d\n", sub());
  return 0;
}

sub.c

#include "sub.h"

int sub(void)
{
  return 1;
}

sub.h

#ifndef SUB_H
#define SUB_H

int sub(void);

#endif
1
You shouldn't define a global variable in a header file.machine_1
sorry this example code is not important. I do not use global var actually. The problem is link does not work fine. If I define sub() in sub.h and delete sub.c, build works fine. So I want to link them in eclipse option. However I cannot find the solution.user8257918

1 Answers

0
votes

I solved. This problem is special in the case of yocto development.

Adding sub.c in Makefile.am enabled to be built fine.

This link helped me. https://community.nxp.com/docs/DOC-106613