Search code examples
cgcccompilation

Compiling C programm with -std=c17 -Wall -Wextra -pedantic error


Im very much new to C, used to Java and Python.

In one of my excercises I have to make sure that my program compiles correctly with the command:

gcc -std=c17 -Wall -Wextra -pedantic file file.c

Unfortunately for me I get these errors:

/usr/bin/ld: file: stdout: invalid version 2 (max 0)
/usr/bin/ld: file: error adding symbols: bad value
collect2: error: ld returned 1 exit status

My problem is that i have only very little ideas what the meaning of these errors could be (and also the added. My code is working perfectly fine. Here is the code:

#include <stdio.h>
#include <unistd.h>

void child_A_proc()
{
  while (1) {
    fprintf(stdout, "%s", "A");
    fflush(stdout);
  }
}

void child_C_proc()
{
  while (1) {
    fprintf(stdout, "%s", "C");
    fflush(stdout);
  }
}

void parent_proc()
{
  while (1) {
    fprintf(stdout, "%s", "B");
    fflush(stdout);
  }
}

int main(void)
{
  int child_A;
  int child_C;

  child_A = fork();                 /* neuen Prozess starten                    */
  if (child_A == 0){                /* Bin ich der erste Kindprozess?           */
    child_A_proc();                 /* ... dann child-Funktion ausfuehren       */
  }else{
    child_C = fork();               /* neuen Prozess starten */
    if (child_C ==0){               /* Bin ich der zweite Kindprozess?          */
        child_C_proc();             /* ... dann child-Funktion ausfuehren       */
    }else{
        parent_proc();              /* ... sonst parent-Funktion ausfuehren     */
    }
  }
  return 0;
}

I tried to find some explanation in the internet but I couldn't replicate the solutions I found to my code. Well at least it didn't help me to understand the problem.


Solution

  • Each non-option filename you give gets compiled as a C file or linked into the binary, depending on its type. (Not sure how the type is determined – I would guess from the file extension.)

    On the command line you gave, there is file and file.c. Probably you meant for file to be the output filename, which should be preceded with -o:

    gcc -std=c17 -Wall -Wextra -pedantic -o file file.c
                                         ^^