Compile project with multiple files . c in Vscode - C language

Asked

Viewed 1,445 times

1

Good afternoon, I’m having trouble separating my code into . c and . h in Vscode.

Follows the implementation:

main. c

#include "head.h"

int main(){
    printar(5);
}

head. h

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

void printar(int x);

head. c

#include "head.h"

void printar(int x){
    printf("\n%d", x);
}

When I try to compile in Vscode, I get the following error:

Undefined Reference to `Printer`

inserir a descrição da imagem aqui

  • Unfortunately no, I don’t know how to configure this part.

  • Your main is being compiled separately. How to compile ?

  • @Isac I am compiling using an extension called C/C++, but it seems that the head. c and head. h files are not being " linked " you know ? In Dev-C++ if I do it like I did in Vscode, it can already do the link " "

  • @Pierrecampos But are you using the Code Runner extension to compile and run ? or are you doing it yourself by the terminal ?

  • @Isac, I’m compiling with C/C++ Compile Run

  • Honestly I do not think it is possible to solve with this extension. If you look at the repository of this extension on github see the following: "Compile & Run single c/c++ files"

  • Yeah, that was my fear, good but thank you for your attention

Show 3 more comments

1 answer

2


The "C/C++ Compile Run" extension you are using only allows you to compile and run one file only, as indicated in own repository:

Compile & Run single c/c++ files easly on vscode

And so with this extension you will not be able to. However you can use the extension Code Runner which will already allow you to compile with multiple files, as well as compile into languages other than C or C++.

After having installed, access the extension settings by clicking on the sprocket of the same and accessing Settings.

On the screen of Settings access the Map Executor edit option, as shown in the following figure:

inserir a descrição da imagem aqui

Add the following block before the } closing file:

,
"code-runner.executorMap": {
    "c": "cd $dir && gcc *.c -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
}

The archive should look similar to the following:

inserir a descrição da imagem aqui

Note that there should be a comma separating the new option from the last one already in the file.

Now just go to the arrow to compile and run:

inserir a descrição da imagem aqui

The option placed in the file compiles all . c that are in the folder you are running in, so you can later change that build statement to another one if you need to. How the execution is by the folder where it is, you must ensure that you have opened in Vscode with "Open Folder" the folder that has your files . c to be compiled.

It is also useful to mention that the added instruction is valid for compiling with gcc, and so you have to adjust it if you are compiling with other compilers like Clang.

Additionally the instruction also assumes that gcc is available in PATH, either windows or linux.

The multi-file build instruction with gcc, the *.c, is not supported in older versions of gcc, and so it is recommended that you have a minimally current version of gcc (something like version 7 or higher).

  • @Pierrecampos You have to have the right folder open in vscode. First you have to do "Open Folder" and open the folder that has your . h and . c

  • I did this, but still occurs the error quoted below in my repost

  • I did as you asked, but I still have the same error, I tried to create another folder too and add the files, but I did not succeed, I am using Windows, maybe there is something that I did not do ?

  • @Pierrecampos In the image you put in the other answer you could see that it was not in the right folder, because it still saw the test folder in the left sidebar. If you are in the right folder you will see the files outside of any folder, thus

  • I understand, I looked in that photo and really was not, but now I left as in your image, it would be [so][1] , but I still get the same error. [1]: https://i.imgur.com/caGzMcE.png

  • @Pierrecampos You continue to do anything wrong, compared to what I said. See the same example now run on Windows: image. I just created a folder, opened that folder in Vscode, created the files with the code and ran straight, with the answer Settings. In the image appear errors, because I did not configure includePath in Vscode, but this does not affect the compile and run.

  • strange, I’ll install Vscode again and see if it works

  • @Pierrecampos What version of your gcc ? open the command line and run gcc --version

  • gcc (tdm64-1) 4.9.2

  • @Pierrecampos This is gcc problem, I here in this windows am with 7.2. I advise you to reinstall a more current version of gcc. By I searched quickly, the wildcard( o *.c) not supported in older versions of gcc.

  • UFA ! It worked now, thank you very much !

  • @Pierrecampos No problem, it cost but it was :)

  • saved my College work ! Compiling in Visual Code is more cool ;) Thank you very much, a hug.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.