Solved

ld: Undefined Symbol

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

685 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