Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[SOLVED]Problems with linking simple program

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
pythagorasmk
Posts: 148
Joined: 2015-01-18 03:40
Been thanked: 2 times

[SOLVED]Problems with linking simple program

#1 Post by pythagorasmk »

I am learning data structures in C. I have written some simple functions for cursor based lists. I have header file list.h and c file list.c with functions in it. The file list.c is compiled without problems.
I have a main program purge.c in which functions from list.c are used. The listing is:

Code: Select all

#include <stdio.h>
#include "list.h"
#include <stdlib.h>

void purge_crs_lst( int_crs_lst *, space_crs * );

int main()
{
  space_crs s;
  initialize_space_crs_lst( &s );
  int_crs_lst l = readlist_crs_lst( &s );
  purge_crs_lst( &l, &s );
  printlist_crs_lst( l, &s );
  l = makenull_crs_lst( &l, &s );
  return 0;
}

void purge_crs_lst( int_crs_lst *l_p, space_crs *s_p )
{
  position_crs_lst p, q;
  p = first_crs_lst( *l_p, s_p );
  while( p != end_crs_lst( *l_p, s_p ) ) {
    q = next_crs_lst( p, *l_p, s_p );
    while( q != end_crs_lst( *l_p, s_p ) )
      if( retrieve_crs_lst( p, *l_p, s_p ) == retrieve_crs_lst( q, *l_p, s_p ) )
	delete_crs_lst( q, l_p, s_p );
      else
	q = next_crs_lst( q, *l_p, s_p );
    p = next_crs_lst( p, *l_p, s_p );
  }
}


When I compile this program with:

Code: Select all

gcc -Wall -o purge list.o purge.c
I get this error message:

Code: Select all

/usr/bin/ld: /tmp/ccaSqwBa.o: in function `main':
purge_crs.c:(.text+0x25): undefined reference to `readlist_crs_lst'
/usr/bin/ld: purge_crs.c:(.text+0x44): undefined reference to `purge_crs_lst'
/usr/bin/ld: /tmp/ccaSqwBa.o: in function `purge_crs_lst':
purge_crs.c:(.text+0xa6): undefined reference to `first_crs_lst'
collect2: error: ld returned 1 exit status
What is the problem?
Last edited by pythagorasmk on 2020-07-14 18:42, edited 1 time in total.

User avatar
ruwolf
Posts: 640
Joined: 2008-02-18 05:04
Location: Banovce nad Bebravou
Has thanked: 40 times
Been thanked: 28 times

Re: Problems with linking simple program

#2 Post by ruwolf »

Errors are in purge_crs.c, but you have posted purge.c.
So, we cannot see source of errors...

pythagorasmk
Posts: 148
Joined: 2015-01-18 03:40
Been thanked: 2 times

Re: Problems with linking simple program

#3 Post by pythagorasmk »

Errors are in purge_crs.c, but you have posted purge.c.
So, we cannot see source of errors...
Sorry my mistake.
The name of the file is purge_crs.c not purge.c. The contents of the purge_crs.c and purge.c is the same. I am compiling purge_crs.c with:

Code: Select all

gcc -Wall -o purge_crs list.o purge_crs.c

LE_746F6D617A7A69
Posts: 932
Joined: 2020-05-03 14:16
Has thanked: 7 times
Been thanked: 68 times

Re: Problems with linking simple program

#4 Post by LE_746F6D617A7A69 »

pythagorasmk wrote:I have header file list.h and c file list.c with functions in it. The file list.c is compiled without problems.

Code: Select all

gcc -Wall -c purge_crs.c -o purge_crs.o
gcc -Wall -c list.c -o list.o
gcc -o purge_crs list.o purge_crs.o
or

Code: Select all

gcc -o purge_crs list.c purge_crs.c
You can't mix compilation with linking.
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed

pythagorasmk
Posts: 148
Joined: 2015-01-18 03:40
Been thanked: 2 times

Re: Problems with linking simple program

#5 Post by pythagorasmk »

LE_746F6D617A7A69 wrote:

Code: Select all

gcc -Wall -c purge_crs.c -o purge_crs.o
gcc -Wall -c list.c -o list.o
gcc -o purge_crs list.o purge_crs.o
or

Code: Select all

gcc -o purge_crs list.c purge_crs.c
You can't mix compilation with linking.
I have tried the two methods you have suggested, but I get the same errors.

LE_746F6D617A7A69
Posts: 932
Joined: 2020-05-03 14:16
Has thanked: 7 times
Been thanked: 68 times

Re: Problems with linking simple program

#6 Post by LE_746F6D617A7A69 »

pythagorasmk wrote:I have tried the two methods you have suggested, but I get the same errors.
It means that there's something wrong with Your code. Check if the function declarations in list.h are matching the definitions in list.c

EDIT: Did You get any errors for this command:

Code: Select all

gcc -Wall -c purge_crs.c -o purge_crs.o
?
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed

pythagorasmk
Posts: 148
Joined: 2015-01-18 03:40
Been thanked: 2 times

Re: Problems with linking simple program

#7 Post by pythagorasmk »

It means that there's something wrong with Your code. Check if the function declarations in list.h are matching the definitions in list.c
Yes, that was the error. Thanks.

LE_746F6D617A7A69
Posts: 932
Joined: 2020-05-03 14:16
Has thanked: 7 times
Been thanked: 68 times

Re: Problems with linking simple program

#8 Post by LE_746F6D617A7A69 »

pythagorasmk wrote:
It means that there's something wrong with Your code. Check if the function declarations in list.h are matching the definitions in list.c
Yes, that was the error. Thanks.
So, please mark the thread as [solved]
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed

Post Reply