Thread: Help with GCC arguments

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    8

    Help with GCC arguments

    Hi,

    I am trying to compile a program that reads and writes to a text file (found it on
    C File I/O

    Code:
    #include <stdio.h>
    
    main() {
    
       FILE *fp;
       char buff[255];
    
       fp = fopen("/tmp/test.txt", "r");
       fscanf(fp, "%s", buff);
       printf("1 : %s\n", buff );
    
       fgets(buff, 255, (FILE*)fp);
       printf("2: %s\n", buff );
       
       fgets(buff, 255, (FILE*)fp);
       printf("3: %s\n", buff );
       fclose(fp);
    }
    I am using the following command to compile and execute it:

    gcc main.c -o demo -lm -pthread -lgmp -lreadline 2>&1 && ./demo

    On Antergos it compiles fine, with gcc version 6.2.1

    On Lubuntu 16.04 it returns the following error:

    /usr/bin/ld: cannot find -lgmp
    /usr/bin/ld: cannot find -lreadline
    collect2: error: ld returned 1 exit status


    How do I fix this?
    What is the meaning of the arguments -lgmp -lreadline 2>&1 ?
    Last edited by NunoF; 11-27-2016 at 06:23 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Well gmp and readline are separate libraries you (or your system administrator) need to install.

    > -lgmp -lreadline
    It's just like the -lm argument. It instructs the linker (that is /usr/bin/ld) to search those libraries for definitions which your program might need. I guess your test program doesn't actually need any of these things.

    The 2>&1 notation is a shell redirection operator, which in this instance redirects the sderr stream onto the stdout stream.
    I/O Redirection
    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.

  3. #3
    Registered User
    Join Date
    Aug 2016
    Posts
    8
    Quote Originally Posted by Salem View Post
    Well gmp and readline are separate libraries you (or your system administrator) need to install.

    > -lgmp -lreadline
    It's just like the -lm argument. It instructs the linker (that is /usr/bin/ld) to search those libraries for definitions which your program might need. I guess your test program doesn't actually need any of these things.

    The 2>&1 notation is a shell redirection operator, which in this instance redirects the sderr stream onto the stdout stream.
    I/O Redirection

    Thanks,

    What is the name of the libraries to install?

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by NunoF View Post
    What is the name of the libraries to install?
    You don't need to install any of those libraries since your program is not using them.

    Your compile command can simply be:
    gcc -o demo main.c

    Then run it:
    ./demo

    All those -l flags and the -pthread flag aren't needed for your program. The -l flags tell the linker the names of non-standard libraries being used (and on some systems the math library). But your program doesn't need any of them.

    The reason the compiler is complaining is that you don't have some of those libraries on that machine, which isn't a problem since you aren't using them and if you ever need them they can be easily installed.

    A better compile command, with a more recent C standard and higher warning level, is:
    gcc -std=c99 -Wall -o demo main.c

  5. #5
    Registered User
    Join Date
    Aug 2016
    Posts
    8
    Many thanks!

    You don't need to install any of those libraries since your program is not using them.

    Your compile command can simply be:
    gcc -o demo main.c

    Then run it:
    ./demo

    A better compile command, with a more recent C standard and higher warning level, is:
    gcc -std=c99 -Wall -o demo main.c
    I don't know why, but using the commands you suggested above I get gybrish on my Lubuntu system. The terminal outputs:

    1 : �Z��
    2: �Z��
    3: �Z��

    What the hell? It appears that the functions fscanf and fgets aren't doing their job properly... How do I get this to work?

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Presumably the file wasn't opened properly? Does /tmp/test.txt exist? Maybe change it to test.txt in the current directory. A program should always test to see if a file opened properly:
    Code:
    #include <stdio.h>
    #include <stdlib.h>  // exit()
    
    int main() {           // main returns int
       FILE fp = fopen("test.txt", "r");
       if (fp == NULL) {
           perror("fopen");
           exit(EXIT_FAILURE);
       }
       
       char buff[255];
       fscanf(fp, "%s", buff);
       printf("1: %s\n", buff);
     
       fgets(buff, 255, (FILE*)fp);
       printf("2: %s\n", buff );
        
       fgets(buff, 255, (FILE*)fp);
       printf("3: %s\n", buff );
    
       fclose(fp);
       
       return 0;    // returning 0 means success
    }
    EDIT:
    Now that I think about it, I guess the file is opening. Are you sure it contains text? Try a different file, perhaps the source file itself.
    Last edited by algorism; 11-28-2016 at 02:17 PM.

  7. #7
    Registered User
    Join Date
    Aug 2016
    Posts
    8
    EDIT:
    Now that I think about it, I guess the file is opening. Are you sure it contains text?
    Yep! The problem was that the file was empty. Sorted now. Thank you!

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Actually, each read of the file needs a check to test if the end-of-file was reached. So your program might be written like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
       FILE *fp = fopen("gcc_args.c", "r");
       if (fp == NULL) {
           perror("fopen");
           exit(EXIT_FAILURE);
       }
       
       char buf[255];
       int i = 0;
    
       while (fgets(buf, sizeof buf, fp) != NULL)
           printf("%3d: %s", ++i, buf);
    
       fclose(fp);
       
       return 0;
    }

  9. #9
    Registered User
    Join Date
    Aug 2016
    Posts
    8
    Thanks! That helped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arguments
    By Suudsu2200 in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2007, 03:10 PM
  2. passing arguments using "Command Line Arguments"
    By Madshan in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 03:46 PM
  3. Few arguments
    By caroundw5h in forum C Programming
    Replies: 8
    Last Post: 07-22-2004, 11:08 PM
  4. Arguments
    By Driveway in forum Windows Programming
    Replies: 3
    Last Post: 07-01-2002, 02:12 PM
  5. arguments
    By mungyun in forum Linux Programming
    Replies: 1
    Last Post: 04-10-2002, 05:29 AM

Tags for this Thread