Results 1 to 7 of 7

Thread: ld returned 1 exit status

  1. #1
    Join Date
    Oct 2014
    Beans
    11

    ld returned 1 exit status

    This is my first post on this forum so kindly overlook any mistake that I make.

    I was working on a very simple problem: taking a string value from the user and printing its lowercase form.

    Here's my code:

    #include <stdio.h>
    #include <string.h>

    main()
    {
    char s[100];
    printf("Enter a Sttring: ");
    gets(s);
    strlwr(s);
    printf("%s",s);
    }

    I am working on ubuntu 14.04. I used the text editor Sublime text 2.

    I tried to compile the program in the terminal with the code: make eg

    It returned:


    eg.c.text+0x3f): undefined reference to `strlwr'
    collect2: error: ld returned 1 exit status
    make: *** [eg] Error 1

    I even tried with gcc and clang but ended up getting the same error.

    Thanks in advance.

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: ld returned 1 exit status

    If you compile with more warnings:

    warning: implicit declaration of function ‘strlwr’ [-Wimplicit-function-declaration]
    In other words, strlwr() doesn't exist (it's not standard C).
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  3. #3
    Join Date
    Sep 2014
    Beans
    5

    Lightbulb Re: ld returned 1 exit status

    Quote Originally Posted by Ayush_Agrawal View Post
    This is my first post on this forum so kindly overlook any mistake that I make.

    I was working on a very simple problem: taking a string value from the user and printing its lowercase form.

    Here's my code:

    #include <stdio.h>
    #include <string.h>

    main()
    {
    char s[100];
    printf("Enter a Sttring: ");
    gets(s);
    strlwr(s);
    printf("%s",s);
    }

    I am working on ubuntu 14.04. I used the text editor Sublime text 2.

    I tried to compile the program in the terminal with the code: make eg

    It returned:


    eg.c.text+0x3f): undefined reference to `strlwr'
    collect2: error: ld returned 1 exit status
    make: *** [eg] Error 1

    I even tried with gcc and clang but ended up getting the same error.

    Thanks in advance.

    There's no such function as strlwr, unless you write it (?), so you're going to have to loop through the
    string in your array (rather sparsely name s[]) and call tolower() on each char (#include <ctype.h> for that function)

    That will do the job.

  4. #4
    Join Date
    Oct 2014
    Beans
    11

    Re: ld returned 1 exit status

    Thank you guys for your valuable response.

    I know what you are getting at but my point is why am I not able to user the particular function strlwr() although it is defined in the string.h header file.

    Actually, in our college they use DevC++ as an IDE and we are taught thorugh it. The same code works perfectly fine in it. I encountered a similar problem with the functions defined in math.h so I googled it. I came up with an answer to link the math.h file during compilation. So the compilation code would look like gcc -o eg eg.c -lm and it all worked fine. So I was wanting to know what to link so that I can use the string functions.

    Sorry for missing out on these details earlier.

  5. #5
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: ld returned 1 exit status

    From the error message you get, it's not(*)... it may be defined in some compilers that have added it to their runtime library, but since it's not standard C it is not defined in GCC. DevC++ is a Windows thing that uses the Microsoft runtime library, that may have additional functions.

    (*)"warning: implicit declaration of function ‘strlwr’ [-Wimplicit-function-declaration]" means the function hasn't been declared (which implies it hasn't been found in the files you included) and the compiler assumes a default definition (which is usually sufficiently wrong to introduce nasty bugs, when the function actually exists).
    Last edited by ofnuts; October 2nd, 2014 at 08:35 AM.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  6. #6
    Join Date
    Apr 2012
    Beans
    7,256

    Re: ld returned 1 exit status

    FYI adding the -lm compiler flag does not "link the math.h file during compilation" - it links the libm math library during linking.

    If you are learning development imho it is important to understand the difference between the two phases (compilation versus linking) and the associated types of errors you are likely to see (e.g. declaration errors versus definition errors).

    [Although in this case the object (function) is neither declared nor defined, for the reasons mentioned by ofnuts.]

  7. #7
    Join Date
    Sep 2014
    Beans
    5

    Re: ld returned 1 exit status

    It looks like it really doesn't exist on most Unix compilers - I've checked OS X and Linux

    The best thing for you to do is look through the string.h header file on the machine you're trying to compile on.

    Looking at the MSDN website: http://msdn.microsoft.com/en-us/library/ms235455.aspx

    ... states that it's a deprecated POSIX function. In that case maybe it's declared in your string.h but you have to #define one of the posix conformance macros
    to enable it.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •