bhanuprakash
bhanuprakash

Reputation: 149

Error while compiling C program with gcc in VSCode

I wanted to compile the below code in VS Code, but I'm fetching this error using "code runner". I've looked up everywhere, but it didn't solve my issue.

I want to implement this T(n) = 2T(n/2) + nlog(n)

q2.c

// b. T(n) = 2T(n/2) + nlog(n)

#include <stdio.h>
#include <math.h>

int func(double n)
{
    return (2*func(n/2) + n*(log(n)));

}

int main()
{
    double n, result;
    printf("Enter the value of 'n' \n");
    scanf("%lf",&n);
    printf("Hey");
    result = func(n);
    printf("%lf \n",result);
    printf("Hey");

    return 0;

}

Console:

user@user-H310M-DS2:~/Desktop/C programming/Assignments$ cd "/home/user/Desktop/C programming/Assignments/" && gcc q2.c -o q2 && "/home/user/Desktop/C programming/Assignments/"q2
/tmp/ccnNXN3L.o: In function `func':
q2.c:(.text+0x3a): undefined reference to `log'
collect2: error: ld returned 1 exit status

Upvotes: 2

Views: 6854

Answers (3)

bhanuprakash
bhanuprakash

Reputation: 149

Just a tweak to code runner's settings.json under file->preferences->settings of VS Code :

I've added the below line

"code-runner.executorMap": 
    {
        "c": "cd $dir && gcc -Wall -Wextra -g  $fileName -lm -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    }

It's working now.

Upvotes: 1

Visual studio code has nothing to do with your issue, you are not compiling with it. Because it is an IDE (or source code editor), not a compiler. I guess you are using it on some Linux or POSIX system. BTW my preferred source code editor is GNU emacs. So your IDE is running some compilation commands (and you need to understand which ones and what these commands are doing). You could run these commands in a terminal (and that actually might be simpler).

As your console logs shows, you are compiling with GCC. Some gcc command has been started (by Visual studio code probably).

Read carefully about Invoking GCC. Order of arguments matters a lot!

You should compile your code with

gcc -Wall -Wextra -g q2.c -lm -o q2

Let me explain this a bit:

  • gcc is your compiler front-end (the actual compiler is cc1 but you never use it directly; you ask gcc to run it)

  • -Wall asks for almost all warnings

  • -Wextra asks for extra warnings. You'll be happy to get them

  • -g asks for debugging information in DWARF. You really want to be able to use the gdb debugger, and gdb practically needs debugging information.

  • q2.c is the source file of your sole translation unit

  • -lm is for your math library. You are using log(3) and its documentation mention that.

  • -o q2 tells gcc to put the executable in q2 (the actual work is done by the ld linker invoked by gcc)

How to configure visual studio code to use that command is your business. You could otherwise type the above command in a terminal. Then you can run your q2 program by typing ./q2 in a terminal for your shell (and you could use gdb on it).

Notice that gcc is starting other programs (like cc1, as, ld). If you want to understand which ones, insert -v after gcc in the command above.

Be sure to read the documentation of every function you are using (so read printf(3), scanf(3), log(3) at least...) and of every program you are using (e.g. of gcc and of Visual studio code).

Once you'll write bigger programs made of several translation units (e.g. foo.c, bar.c, gee.c), you would want to use some build automation tool (because compiling all of them every time with gcc -Wall -Wextra -g foo.c bar.c gee.c -lm -o qqq is possible, but inconvenient). You could learn to use GNU make (or ninja).

Read How to debug small programs. Don't expect your program to work as you want at first.

BTW, study the source code of some existing free software programs (but start with simple projects, e.g. on github, of less than a hundred thousand lines). This could teach you many useful things.

Upvotes: 4

iBug
iBug

Reputation: 37217

I'm not sure how VSCode compiles programs, but since it uses GCC, it's likely that you need to link the math library libm when compiling, by supplying an argument -lm to GCC.

Upvotes: 2

Related Questions