0

I never programmed in C before, but I need a C program that kills a process given its PID. I know this question was a lot asked, but I encounter a compilation error that I don't understand. Here is the code built with the things that I could find on the Internet:

#include<stdio.h>
#include<signal.h>

int main (int argc, char *argv[]){

    if (argc != 2 ) return 1;

    int pid = atoi (argv[1]);
    kill (pid, SIGINT);

    return 0;
}

And the logs:

-------------- Build: Debug in processKiller (compiler: GNU GCC Compiler)---------------

mingw32-gcc.exe -Wall -g  -c "D:\.Ce PC\Desktop\ws processKiller\processKiller\main.c" -o obj\Debug\main.o
D:\.Ce PC\Desktop\ws processKiller\processKiller\main.c: In function 'main':
D:\.Ce PC\Desktop\ws processKiller\processKiller\main.c:8:5: warning: implicit declaration of function 'atoi' [-Wimplicit-function-declaration]
     int pid = atoi (argv[1]);
     ^
D:\.Ce PC\Desktop\ws processKiller\processKiller\main.c:9:5: warning: implicit declaration of function 'kill' [-Wimplicit-function-declaration]
     kill (pid, SIGINT);
     ^
mingw32-g++.exe  -o bin\Debug\processKiller.exe obj\Debug\main.o   
obj\Debug\main.o: In function `main':
D:/.Ce PC/Desktop/ws processKiller/processKiller/main.c:9: undefined reference to `kill'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 2 warning(s) (0 minute(s), 0 second(s))

Thanks.

Cl00e9ment
  • 773
  • 1
  • 13
  • 30

1 Answers1

1

kill is a POSIX function. Since you are on Windows, you will need to find how to kill a process using WINAPI. This is a bit more complicated but this should get you started: How to effectively kill a process in C++ (Win32)?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
dangee1705
  • 3,445
  • 1
  • 21
  • 40