Linux and C Programming "undefined reference to function"

Posted on 2004-09-05
Medium Priority
Last Modified: 2010-04-22

I've been working on the linux kernel itself. Let' me describe the problem. It's kind of long-winded.

I have modified the open.c file by adding in the following lines

      #include <linux/bsm/audit-CHDIR.c>
      return error;

and at the beginning of this modified open.c file, I've added

#include <linux/bsm/audit.h>

Inside the audit-CHDIR.c file, we have the line

      audit(&header, &event);

and inside the audit.h file, we have the line

int audit(audit_header*, void*);

All is well so far, until I compile the code. Apparently, compilation works fine because the audit function is declared in the audit.h header file. but an error occurred during the linking stage ( i assume this error message only occurs at linking stage)

fs/fs.o(.text+0x764): In function `sys_chdir':
: undefined reference to `audit'

The reason I can think of is that there is no way for the build system to know where audit function is DEFINED. The compiler knows that audit function is DECLARED in the audit.h file, but the linker has no idea that audit function is defined in /usr/src/linux/kernel/audit.c. And I don't know how to let the linker know about this.

Thanks for your help.

Question by:feelite
  • 2

Expert Comment

ID: 11987248
You would probably need to add a depency on audit.o in the Makefile, this will translate to a make auto-rule which will compile auto.c and link it in. I only have the 2.6.5 kernel source handy right now, but in that you'd probably want to edit /usr/src/linux/kernel/Makefile, and add audit.o to the obj-y line right at the beginning of the file.

Author Comment

ID: 11993399
hi, thanks for reply. (btw, i'm using 2.4 kernel, FC1)

The only change I made to /usr/src/linux/kernel/Makefile is the addition of the following lines

ifeq ($(CONFIG_BSM_AUDIT),y)
OX_OBJS += audit.o

I suppose it tells something about looking for the audit.o while linking this kernel.o. However, I'm new to the GNU Make system, the manual at the official site does not talk about things like OX_OBJS and stuff.

Also, how does the make system know where to look for the audit.o?

The full /usr/src/linux/kernel/Makefile reads the following

# Makefile for the linux kernel.
# Note! Dependencies are done automagically by 'make dep', which also
# removes any old dependencies. DON'T put your own dependencies here
# unless it's something special (ie not a .c file).
# Note 2! The CFLAGS definitions are now in the main makefile...

O_TARGET := kernel.o

export-objs = signal.o sys.o kmod.o context.o ksyms.o pm.o exec_domain.o printk.o cpufreq.o profile.o module.o

obj-y     = sched.o dma.o fork.o exec_domain.o panic.o printk.o profile.o \
          module.o exit.o itimer.o info.o time.o softirq.o resource.o \
          sysctl.o acct.o capability.o ptrace.o timer.o user.o \
          signal.o sys.o kmod.o context.o \
          futex.o pid.o kksymoops.o

obj-$(CONFIG_UID16) += uid16.o
obj-$(CONFIG_MODULES) += ksyms.o
obj-$(CONFIG_PM) += pm.o
obj-$(CONFIG_CPU_FREQ) += cpufreq.o
obj-$(CONFIG_IKCONFIG) += configs.o
obj-$(CONFIG_KALLSYMS) += kallsyms.o

ifeq ($(CONFIG_BSM_AUDIT),y)
OX_OBJS += audit.o

ifneq ($(CONFIG_IA64),y)
# According to Alan Modra <alan@linuxcare.com.au>, the -fno-omit-frame-pointer is
# needed for x86 only.  Why this used to be enabled for all architectures is beyond
# me.  I suspect most platforms don't need this, but until we know that for sure
# I turn this off for IA-64 only.  Andreas Schwab says it's also needed on m68k
# to get a correct value for the wait-channel (WCHAN in ps). --davidm
# Some gcc's are building so that O(1) scheduler is triple faulting if we
# build -O2. (Turns out to be a CPU issue. Update your microcode if you hit it)
CFLAGS_sched.o := $(PROFILING) -fno-omit-frame-pointer

include $(TOPDIR)/Rules.make

configs.o: $(TOPDIR)/scripts/mkconfigs configs.c
      $(CC) $(CFLAGS) $(CFLAGS_KERNEL) -DEXPORT_SYMTAB -c -o configs.o configs.c

$(TOPDIR)/scripts/mkconfigs: $(TOPDIR)/scripts/mkconfigs.c
      $(HOSTCC) $(HOSTCFLAGS) -o $(TOPDIR)/scripts/mkconfigs $(TOPDIR)/scripts/mkconfigs.c

configs.c: $(TOPDIR)/.config $(TOPDIR)/scripts/mkconfigs
      $(TOPDIR)/scripts/mkconfigs $(TOPDIR)/.config configs.c

Accepted Solution

gysbert1 earned 400 total points
ID: 11993842
GNU make defines a number of auto rules for making certain things, there are options to list them and override them but I can't remember how. One of the auto-rules tells make how to generate an object file from a c file, so if you have a .o dependency somewhere which doesn't exist, it will look for a corresponding .c and compile it to an object file automatically.

OX_OBJS is nothing specific to GNU make AFAIK, it is just a variable to which you have added audit.o. I don't have FC1 or 2.4.x anymore, but I can't see OX_OBJS being used in the 2.6 makefile, I would have thought you should say something like
obj-y += audit.o
but I don't know the kernel make system well at all.
BTW, this
is a very good GNU make reference/tutorial IMHO.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question