How to define a undefinded reference to a function

2020-05-02 13:26发布

问题:

I am working on two Beaglebone Black with Xenomai and RTnet. I have two c-files for a roundtrip ethernet frame between the BBB's. When I try to compile the first c-file there occur some errors:

undefined reference to 'rt_task_self'

rt_task_self is a function in my c-file and is declared in my headerfile "task.h". So in my opinion "undefined" means that it is just not defined in any cpp-file "task.cpp" for the headerfile "task.h".

But I am a little bit confused: How do I tell my program that my headerfile "task.h" is defined in my other file "task.cpp" or "task.o" or... I have many header files in my C-file but only error with my "task.h" file and I do not see any differences in the #include rows between my "task.h" and all the other header files.

Part of Roundtrip C-file:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>

/*XENOMAI*/
#include "task.h"
#include <rtdm/rtdm.h>
#include <asm/ioctl.h>

#define SERVER "192.168.127.10"
#define BUFLEN 512
#define PORT 8888

void die(char *s)
{
    perror(s);
    exit(1);
}

Part of task.h:

#ifndef _XENO_TASK_H
#define _XENO_TASK_H

#include <nucleus/sched.h>
#include <native/types.h>

/* Creation flags. */
#define T_FPU     XNFPU
#define T_SUSP    XNSUSP
/* <!> High bits must not conflict with XNFPU|XNSHADOW|XNSUSP. */
#define T_CPU(cpu) (1 << (24 + (cpu & 7))) /* Up to 8 cpus [0-7] */
#define T_CPUMASK  0xff000000

Part of another headerfile in the roundtrip c-file:

#ifndef _RTDM_H
#define _RTDM_H

#ifdef __KERNEL__

#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/ioctl.h>
#include <linux/sched.h>
#include <linux/socket.h>

typedef u32 socklen_t;
typedef struct task_struct rtdm_user_info_t;

#else /* !__KERNEL__ */

#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

My makefile:

# Allow overriding xeno-config on make command line
XENO_CONFIG=xeno-config

prefix := $(shell $(XENO_CONFIG) --prefix)

ifeq ($(prefix),)
$(error Please add <xenomai-install-path>/bin to your PATH variable)
endif

CC := $(shell $(XENO_CONFIG) --skin=posix --cc)
STD_CFLAGS  := $(shell $(XENO_CONFIG) --skin=posix --cflags) -g
STD_LDFLAGS := $(shell $(XENO_CONFIG) --skin=posix --ldflags) -g -lrtdm

STD_TARGETS := rtt_rt

all: $(STD_TARGETS)

$(STD_TARGETS): $(STD_TARGETS:%=%.c)
    $(CC) -o $@ $< $(STD_CFLAGS) $(STD_LDFLAGS)

clean:
    $(RM) -f *.o *~ $(STD_TARGETS)