Solved

ld: Undefined Symbol

Posted on 1997-12-26
1
913 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 nested-loops in the C programming language.

919 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now