0

I want to run my multi-file C project in VSC using the CodeRunner extension instead of having to use the command line to create an executable. I want to see the output directly in the VSC terminal window.

When I try to do Run Code on my main file, I get the following error message:

C:\Users\223094~1\AppData\Local\Temp\cciAWyDb.o:main.c:(.text+0x1e): undefined reference to `foo'
collect2.exe: error: ld returned 1 exit status

I am able to get the code to run through the command line by running gcc main.c foo.c -o program and then ./program which gives me the correct output.

I have attached my code below:

//main.c
#include "foo.h"

int main(void)
{
    foo(42, "bar");
    return 0;
}
//foo.c
#include "foo.h"    /* Always include the header file that declares something
                     * in the C file that defines it. This makes sure that the
                     * declaration and definition are always in-sync.  Put this
                     * header first in foo.c to ensure the header is self-contained.
                     */
#include <stdio.h>
                       
/**
 * This is the function definition.
 * It is the actual body of the function which was declared elsewhere.
 */
void foo(int id, char *name)
{
    printf("Hello World");
    fprintf(stderr, "foo(%d, \"%s\");\n", id, name);
    /* This will print how foo was called to stderr - standard error.
     * e.g., foo(42, "Hi!") will print `foo(42, "Hi!")`
     */
}

//foo.h
#ifndef FOO_DOT_H    /* This is an "include guard" */
#define FOO_DOT_H    /* prevents the file from being included twice. */
                     /* Including a header file twice causes all kinds */
                     /* of interesting problems.*/

/**
 * This is a function declaration.
 * It tells the compiler that the function exists somewhere.
 */
void foo(int id, char *name);

#endif /* FOO_DOT_H */
Armandas
  • 2,276
  • 1
  • 22
  • 27
palacetrading
  • 71
  • 1
  • 11
  • 2
    use a build tool: `Make`, `CMake`, `MSBuild`, .... – rioV8 Feb 28 '23 at 20:05
  • Check this: https://stackoverflow.com/questions/59474537/code-runner-configuration-for-running-multiple-cpp-classes-in-vscode/75597472#75597472 – Armandas Feb 28 '23 at 22:02

0 Answers0