Thread: extern function

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    51

    extern function

    My extern function code is not working as it suppose to do. when I run the code I get error

    ld returned 1 exit status

    Code:
    file1.c
    
    extern int add(int a, int b) 
    {
        int c; 
        c = a+b;
        return c; 
    }
    
    
    file1.h 
    extern int add(int a, int b);
    
    
    main.c
    #include<stdio.h>
    #include"file1.h"
    
    
    int main() 
    {
      int result;
      result = add (4, 5);
      
      printf("%d\n", result);
      
      return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,069
    file1.c
    Code:
    extern int add(int a, int b) 
    {
        int c; 
        c = a+b;
        return c; 
    }
    The above extern is incorrect and needs to be removed!

    Please read a book about what "extern" means and how and where to use it.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,734
    > ld returned 1 exit status
    You forgot to compile file.c

    gcc file1.c main.c
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2021
    Posts
    51
    Quote Originally Posted by stahta01 View Post
    file1.c
    Code:
    extern int add(int a, int b) 
    {
        int c; 
        c = a+b;
        return c; 
    }
    The above extern is incorrect and needs to be removed!

    Tim S.
    What's incorrect I'm following link What is an `extern` function in C?

    extern int incr(int);extern int add(int a, int b) { return a+b; }
    Last edited by Rahul11; 08-23-2021 at 01:36 AM.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,069
    Nop, you are not understanding that an function prototype is not an full function.

    Edit: The website says it is possible; but, I think an C expert would say it may just be allowed by some C compilers.

    You are able to use "extern" before an function prototype; some people consider this good style like the website you linked.
    You cannot use "extern" before the full function.

    Tim S.
    Last edited by stahta01; 08-23-2021 at 02:24 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by stahta01 View Post
    Nop, you are not understanding that an function prototype is not an full function.

    Edit: The website says it is possible; but, I think an C expert would say it may just be allowed by some C compilers.

    You are able to use "extern" before an function prototype; some people consider this good style like the website you linked.
    You cannot use "extern" before the full function.

    Tim S.
    Plus, Rahul11, you need to get hold of one of the three books I listed above, and study it thoroughly!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,351
    Quote Originally Posted by stahta01
    You cannot use "extern" before the full function.
    That's not true. It's just rarely used since it is the default. Here's a valid example from the C standard:
    Code:
    extern int max(int a, int b)
    {
        return a > b ? a : b;
    }
    Quote Originally Posted by rstanley
    Plus, Rahul11, you need to get hold of one of the three books I listed above, and study it thoroughly!
    You didn't actually list any books above. Rahul11 started two topics on extern, one for variables and one for functions; you listed the books in the topic on extern for variables.

    Anyway, the problem should be fixed by what Salem suggested in post #3.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2021
    Posts
    51
    Quote Originally Posted by Salem View Post
    > ld returned 1 exit status
    You forgot to compile file.c

    gcc file1.c main.c
    main.c
    Code:
    #include<stdio.h>#include"file1.h"
     
     
    int main() 
    {
      int result;
      result = add (4, 5);
       
      printf("%d\n", result);
       
      return 0;
    }
    file1.c
    Code:
    extern int add(int a, int b){
        return a > b ? a : b;
    }
    file1.h

    Code:
    extern int add(int a, int b);
    gcc file1.c main.c

    When I ran code I get x=1 x=4

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,734
    Those results don't come from that code.
    And naming your function 'add' when it returns 'max' is utterly misleading.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    May 2021
    Posts
    51
    Quote Originally Posted by Salem View Post
    Those results don't come from that code.
    And naming your function 'add' when it returns 'max' is utterly misleading.
    main.c
    Code:
    #include<stdio.h>
    #include"file1.h"
     
    int main() 
    {
      int result;
      result = max (4, 5);
       
      printf("%d\n", result);
       
      return 0;
    }
    file1.c
    Code:
    extern int max(int a, int b){
        return a > b ? a : b;
    }
    file1.h
    Code:
    extern int max(int a, int b);
    gcc file1.c main.c

    warning: implicit declaration of function 'max' [-Wimplicit-function-declaration]
    result = max (4, 5);
    ^~~
    x=1 x=4

    how to remove warning in code ?
    why result is wrong?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,734
    A couple of things come to mind.
    1. You don't include file1.h in file1.c
    2. max is such a common name that you may have tripped over some other declaration. Try naming it say 'myMax'.

    Also, it works for me
    Code:
    // file1.c
    extern int max(int a, int b){
        return a > b ? a : b;
    }
    // file1.h
    extern int max(int a, int b);
    
    // main.c
    #include<stdio.h>
    #include"file1.h"
    
    int main()
    {
      int result;
      result = max (4, 5);
    
      printf("%d\n", result);
    
      return 0;
    }
    $ gcc file1.c main.c
    $ gcc -Wall file1.c main.c
    $ gcc -Wall -Wextra file1.c main.c
    $ ./a.out 
    5
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    May 2021
    Posts
    51
    Quote Originally Posted by Salem View Post
    A couple of things come to mind.
    1. You don't include file1.h in file1.c
    2. max is such a common name that you may have tripped over some other declaration. Try naming it say 'myMax'.

    Also, it works for me
    I think I am having issue with complier

    My setup : Mingw GCC windows
    Folder Extern
    file1.c file1.h main.c


    gcc file1.c main.c
    main.c: In function 'main':
    main.c:8:12: warning: implicit declaration of function 'MyMax' [-Wimplicit-function-declaration]
    result = MyMax (4, 5);
    ^~~~~
    C:\Users\~1\AppData\Local\Temp\ccn9ySO4.o:main.c.text+0x1e): undefined reference to `MyMax'
    collect2.exe: error: ld returned 1 exit status

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,734
    Well yeah, you have the change the name in ALL the places.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    May 2021
    Posts
    51
    Quote Originally Posted by Salem View Post
    Well yeah, you have the change the name in ALL the places.
    This command work for me
    gcc -o hello file1.c main.c

  15. #15
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by Rahul11 View Post
    This command work for me
    gcc -o hello file1.c main.c
    I would also add warning options to your compile command:
    Code:
     gcc -o hello -Wall -Wextra -Wpedantic file1.c main.c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-25-2017, 10:25 AM
  2. Using extern on function so it can be used in other files
    By deathslice in forum C Programming
    Replies: 3
    Last Post: 02-29-2016, 12:59 PM
  3. In .h files, should function declarations be 'extern' ?
    By Richardcavell in forum C Programming
    Replies: 2
    Last Post: 02-28-2011, 07:56 AM
  4. question about declaring function extern
    By movl0x1 in forum C Programming
    Replies: 2
    Last Post: 07-25-2007, 05:16 AM
  5. extern outside of a function
    By spank in forum C Programming
    Replies: 7
    Last Post: 07-10-2007, 09:43 AM

Tags for this Thread