简体   繁体   中英

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

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 (eg of gcc and of Visual studio code ).

Once you'll write bigger programs made of several translation units (eg 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, eg on github , of less than a hundred thousand lines). This could teach you many useful things.

我不确定VSCode如何编译程序,但由于它使用GCC,因此可能需要在编译时通过向GCC提供参数-lm来链接数学库libm

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM