Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
gcc compile multiple files

I have these five source

main.c
src_print1.c
src_print2.c
header_print1.h
header_print2.h

the contents are simple and are as following for respective files:

main.c

#include "header_print1.h"
#include "header_print2.h"

int main(int argc, char** argv) {
    print1();
    print2();
    return 0;
}

header_print1.h

#ifndef PRINT_1
#define PRINT_1

#include <stdio.h>
#include <stdlib.h>

void print1();

#endif

header_print2.h

#ifndef PRINT_2
#define PRINT_2

#include <stdio.h>
#include <stdlib.h>

void print2();

#endif

src_print1.c

#include "header_print1.h"

void print1() {
   printf("Hello 1\n");
}

src_print2.c

#include "header_print2.h"

void print2() {
   printf("Hello 2\n");
}

Using gcc I have tried to compile using the following command line:

  gcc -I ./ -o test -c main.c src_print1.c src_print2.c

Everything is in the same folder. The error I get is:

gcc: cannot specify -o with -c or -S with multiple files

I looked up at gcc manual, but actually I don't understand what to do in this case, since usually I use IDE and not the command line.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

IMHO, if you rewrite your compilation statement like

 gcc -I./ -o test main.c src_print1.c src_print2.c

You'll be good to go. There is no need for -c flag [NOTE] when you're specifying the output binary using -o.

Also, as mentioned here, all the files are in same directory, you can even shorten the statement as

 gcc -o test main.c src_print1.c src_print2.c

Suggestion: While the above change(s) will do the job, this is not considered an elegant way of doing so. Please consider creating a makefile which will make your life easier.


[Note]:

Regarding the -c option, as per the online gcc manual, (emphasis mine)

-c
Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.

So, it should be clear by now, why you got the error.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda