Solved

ld: Undefined Symbol

Posted on 1997-12-26
1
911 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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

771 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

14 Experts available now in Live!

Get 1:1 Help Now