Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #97631

Re: Can't link a simple header file to an even more simple program

From Ike Naar <ike@iceland.freeshell.org>
Newsgroups comp.lang.c
Subject Re: Can't link a simple header file to an even more simple program
Date 2016-11-28 12:48 +0000
Organization A noiseless patient Spider
Message-ID <slrn3vfso3o9vp.48g.ike@iceland.freeshell.org> (permalink)
References <e11bcfb9-8cb4-48c2-89ea-b2586ab4a5f1@googlegroups.com>

Show all headers | View raw


On 2016-11-28, Alla _ <modelling.data@gmail.com> wrote:
> Hello!
>
> I have an issue with a very simple test code (downloaded from net): the compiler shows an error 
> message, which usually would indicate (at least this what I managed to learn about his error message) 
> that I have not added a correct library
>
> Undefined symbols for architecture x86_64:
>   "_function1", referenced from:
>       _main in 02_01-18ac0b.o
> ld: symbol(s) not found for architecture x86_64
> clang: error: linker command failed with exit code 1 (use -v to see invocation)
>
> Originally both the header and the .c file, which defines functions in the header file, didn't have an #include <stdio.h> line; the compiler displayed the above error message. Haven't found any mistake, I have added the stdio.h library, but the compiler still displays the same error message. I don't 
> understand why the header is not being linked. 

Elsethread it has already been explained that you must compile both the main
.c file and myLibrary.c . A few other comments:

> (1) a program:  
>     #include <stdio.h>			

The #include <stdio.h> is unnecessary.

>     #include <stdlib.h>
>
>     #include "myLibrary.h"		
>
>    #define MAX_LENGTH 21.8
>    #define WORK_WEEK  5
>
>    int main(void) {
> 	function1();
> 	return EXIT_SUCCESS;
>   }
>
> (2) header file:
>
>  #include <stdio.h> /** wasn't in the original file, added by me**/

The #include <stdio.h> is unnecessary.

> #ifndef MYLIBRARY_H_
> #define MYLIBRARY_H_
>
> void function1(void);
> void function2(void);
>
> #endif /* MYLIBRARY_H_ */
>
> (3) 
> #include <stdio.h>  /** wasn't in the original file, added by me**/

Here it would be a good idea to #include "myLibrary.h" as well.
This way, the compiler can check that the declararions in 
myLibrary.h and myLibrary.c are not conflicting.

> void function1(void){
> 	puts("It works :)\n");


The "\n" at the string is not necessary, puts() already adds a terminating
newline to the string to be printed.

> }
>
> void function2(void){
> 	//This function does nothing as well
> }

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Can't link a simple header file to an even more simple program Alla _ <modelling.data@gmail.com> - 2016-11-28 02:41 -0800
  Re: Can't link a simple header file to an even more simple program BartC <bc@freeuk.com> - 2016-11-28 11:00 +0000
    Re: Can't link a simple header file to an even more simple program Alla _ <modelling.data@gmail.com> - 2016-11-28 03:17 -0800
  Re: Can't link a simple header file to an even more simple program Alla _ <modelling.data@gmail.com> - 2016-11-28 03:04 -0800
    Re: Can't link a simple header file to an even more simple program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-28 13:39 +0000
  Re: Can't link a simple header file to an even more simple program Ike Naar <ike@iceland.freeshell.org> - 2016-11-28 12:48 +0000
    Re: Can't link a simple header file to an even more simple program Alla _ <modelling.data@gmail.com> - 2016-11-28 08:05 -0800
      Re: Can't link a simple header file to an even more simple program Richard Heathfield <rjh@cpax.org.uk> - 2016-11-28 16:20 +0000
        Re: Can't link a simple header file to an even more simple program Alla _ <modelling.data@gmail.com> - 2016-11-28 09:26 -0800
          Re: Can't link a simple header file to an even more simple program Richard Heathfield <rjh@cpax.org.uk> - 2016-11-28 17:56 +0000
            Re: Can't link a simple header file to an even more simple program Alla _ <modelling.data@gmail.com> - 2016-11-28 10:04 -0800
              Re: Can't link a simple header file to an even more simple program Richard Heathfield <rjh@cpax.org.uk> - 2016-11-28 18:08 +0000
          Re: Can't link a simple header file to an even more simple program "James R. Kuyper" <jameskuyper@verizon.net> - 2016-11-28 12:57 -0500
            Re: Can't link a simple header file to an even more simple program Alla _ <modelling.data@gmail.com> - 2016-11-28 10:10 -0800
              Re: Can't link a simple header file to an even more simple program Richard Heathfield <rjh@cpax.org.uk> - 2016-11-28 18:13 +0000
                Re: Can't link a simple header file to an even more simple program Alla _ <modelling.data@gmail.com> - 2016-11-28 10:17 -0800
              Re: Can't link a simple header file to an even more simple program "James R. Kuyper" <jameskuyper@verizon.net> - 2016-11-28 13:19 -0500
                Re: Can't link a simple header file to an even more simple program Alla _ <modelling.data@gmail.com> - 2016-11-28 10:22 -0800
            Re: Can't link a simple header file to an even more simple program David Thompson <dave.thompson2@verizon.net> - 2016-12-10 22:40 -0500
  Re: Can't link a simple header file to an even more simple program Richard Heathfield <rjh@cpax.org.uk> - 2016-11-28 12:55 +0000
    Re: Can't link a simple header file to an even more simple program Alla _ <modelling.data@gmail.com> - 2016-11-28 08:45 -0800
      Re: Can't link a simple header file to an even more simple program Richard Heathfield <rjh@cpax.org.uk> - 2016-11-28 17:11 +0000
        Re: Can't link a simple header file to an even more simple program Alla _ <modelling.data@gmail.com> - 2016-11-28 09:49 -0800
          Re: Can't link a simple header file to an even more simple program Richard Heathfield <rjh@cpax.org.uk> - 2016-11-28 18:00 +0000
            Re: Can't link a simple header file to an even more simple program Alla _ <modelling.data@gmail.com> - 2016-11-28 10:12 -0800

csiph-web