Thread: How to build multiple C files in VS code

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    How to build multiple C files in VS code

    Hi guys,

    I try to compile a program with a main.c, file.c and file.h files
    The file.c and file.h are in another directory than the main.c
    I included the file.h in both main.c and file.c

    How to build multiple C files in VS code-screenshot_1-png

    How to build multiple C files in VS code-screenshot_2-png

    When I compile the program I get the the undefined reference error of myName(); that is declared in file.h and defined in file.c

    How to build multiple C files in VS code-screenshot_4-jpg

    I followed the suggestion in VS Code documentation to edit the tasks.json but it didn't work. I tried both the .cpp and c extension in the tasks.json. Now the file is like this:

    How to build multiple C files in VS code-screenshot_5-png

    If I build the files from the terminal gcc main.c Library/file.c it works fine

    I do not know what else I am missing. Any suggestion?
    Attached Images Attached Images How to build multiple C files in VS code-screenshot_3-png 
    Last edited by Nikosant03; 04-20-2023 at 09:34 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    It works better if you invoke make to read a makefile.
    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
    Dec 2017
    Posts
    1,664
    You can see in the OUTPUT that it's still just running gcc main.c -o main
    (I don't know what the d:\Programs\Dropbox\... stuff is about.)
    Maybe you need to tell it somewhere to use that json file.

    BTW, in your modifications the *.c should probably just be main.c and the line with file.h shouldn't be there at all (you never need to specify header files to the compiler).
    These changes won't make a difference, though.
    All truths are half-truths. - A.N. Whitehead

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    makfile works fine thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about C++ Splitting your Code into Multiple Files
    By roroprogram in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2021, 03:23 PM
  2. Separating out code into multiple files
    By Richardcavell in forum C Programming
    Replies: 2
    Last Post: 04-24-2011, 05:13 PM
  3. Breaking up source code into multiple files
    By mherald81 in forum C Programming
    Replies: 9
    Last Post: 10-15-2010, 04:55 PM
  4. Replies: 13
    Last Post: 08-12-2010, 01:58 PM
  5. Splitting Code Into Multiple Files
    By pobri19 in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2008, 04:21 AM

Tags for this Thread