Solved

ld: Undefined Symbol

Posted on 1997-12-26
1
915 Views
Last Modified: 2006-11-17
I have a C program I have developed that also has a customized library
file.  When I compile (ANSI Compiler on UNIX), I get the following error:

ld:  Undefined Symbol
     _SHPOpen

Where SHPOpen is a library function I have set up in my library file
( <file>.h ) to handle my project.  I don't have any underscores ( _ )
in front of it, yet an underscore shows up when this error returns.

Any suggestions on how to solve this???  Any help would be appreciated...

Thanks,

Terry
0
Comment
Question by:twl
1 Comment
 
LVL 1

Accepted Solution

by:
SvenT earned 100 total points
ID: 1256842
The underscore is added by the compiler to handle external
references. If I understand your problem correct, you want
to make a program consisting of more than one source file.
When you don't link the files correct, you will get a
"ld: Undefined symbol" error.

I send an example which consists of two source files and
one header file (.h). I list the files, compiles one file
to an object (.o) file, compiles the second file and links
them together to form a program and finally executes the
program. I hope this shows how you can solve your problem.

prompt> cat func.c
#include <stdio.h>
#include "shp.h"

int SHPOpen(int i)
{
        printf("Hi, this is SHPOpen. %d\n", i);
        if (i > MY_DEFINITION)
                printf("The parameter %d is larger than %d\n", i, MY_DEFINITION)
;
        return i;
}

prompt> cat main.c

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

main()
{
        printf("This is main\n");
        SHPOpen(3);
        printf("This is main again\n");
}

prompt>  cat shp.h
#define MY_DEFINITION 5

extern int SHPOpen(int i);

prompt> cc -c func.c
prompt> cc main.c func.o -o myprogram
prompt> myprogram
This is main
Hi, this is SHPOpen. 3
This is main again
prompt>


0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
My eclipse editor won't start ? 4 154
mixing C++ and C code elegantly 10 157
Super Scope, DHCP 5 78
Computer slow / BSOD 10 60
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

809 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