MinGW compilation says header files are missing, which package maybe missing? - c

This is the first gcc error on compilation, copying features.h from the web into the include folder for MinGW helped :
$ gcc -g hello_world.c
In file included from c:\mingw\include\stdio.h:55,
from hello_world.c:1:
c:\mingw\include\_mingw.h:174:10: fatal error: features.h: No such file or directory
#include <features.h>
^~~~~~~~~~~~
compilation terminated.
This is the new error :
$ gcc -g hello_world.c
In file included from c:\mingw\include\_mingw.h:174,
from c:\mingw\include\stdio.h:55,
from hello_world.c:1:
c:\mingw\include\features.h:406:10: fatal error: stdc-predef.h: No such file or directory
#include <stdc-predef.h>
^~~~~~~~~~~~~~~
compilation terminated.
The hello-world file :
#include <stdio.h> int main(int argc, const char *argv[]) {
/* I am C developer */
printf("Hello, Poftut! \n");
return ; }
C:\MinGW\bin\ is in PATH.Pretty sure I have missed some package installation.

Related

fatal error: curl/curl.h: No such file or directory using vcpkg

i have been looking for hours for a solution and i haven't found anything
tried reinstalling curl, reinstalling CMake but nothing worked
i use visual studio code
the error is
fatal error: curl/curl.h: No such file or directory
3 | #include <curl/curl.h>
| ^~~~~~~~~~~~~
compilation terminated.
i use g++ 9.2.0
the code is
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
int main() {
CURL *curl;
CURLcode res;
return 0;
}
compiled it using g++
g++ learning.c -l curl -o test
and the curl path is
C:\vcpkg\packages\curl_x86-windows

Error in compiling before linking the object files using gcc

I have source files written in C programming using notepad++ and I am running them from command lines and later i need to link them inorder to generate the .exe file.
Here are the following commands I want to use while generating .exe file
gcc logc.c -o logc
gcc mainc.c -o mainc
gcc -o output logc.o mainc.o
But when i run the following command my compiler is returning with the following error status.
gcc logc.c -o logc
(x86)/mingw-w64/i686-8.1.0-win32-dwarf-rt_v6-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x39): undefined reference to `WinMain#16'
when i run the following command to compile my mainc file
C:\Users\user\AppData\Local\Temp\ccskY3nf.o:mainc.c:(.text+0x31): undefined reference to `Log'
collect2.exe: error: ld returned 1 exit status
And here are my mainc.c and logc.c and logc.h files for your reference
logc.c file is here
#include <stdio.h>
#include "logc.h"
void InitLog()
{
Log("Initializing Log");
}
void Log(const char* message)
{
printf(" %s",message);
}
mainc.c file is here
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
#include "logc.h"
int main()
{
int x = 5;
bool comparisonResult = x == 5;
if(comparisonResult == 1)
Log("Hello World");
return 0;
}
and logc.h file is here
#ifndef _LOG_H
#define _LOG_H
void InitLog();
void Log(const char* message);
#endif
How can i compile individual source files and then link them and generate an executable file.
Thanks in advance.
You don't create object files, for that you need the -c argument:
gcc logc.c -c
gcc mainc.c -c
gcc -o output logc.o mainc.o
By default gcc will generate an executable file, not an object file. So when you compile logc.c, it tries to make an executable but it can't find the main function so it fails. Similarly with main.c, it tries to make an executable but can't find Log
You need to add the -c option to create object files:
gcc logc.c -c -o logc.o
gcc mainc.c -c -o mainc.o

unable to get preprocessed source file or object file if I include header from a different directory

my folder/file structure:
testing
folder
head.h
main.c
main.c
#include "head.h"
int main(){
foo(3);
return 1;
}
head.h
void foo(int x){
x = 5;
}
in cmd I go to "testing" folder and type in:
gcc -c -lfolder main.c -o main.o
but that gets me an error:
main.c:1:18: fatal error: head.h: No such file or directory
#include "head.h"
From what I understand, flag -ldir_name specifies that compiler should look inside of that folder for any #include files.
Can you please help me undersand what I am doing wrong. I also tried getting preporcessor's output:
gcc -E -lfolder main.c -o main.i
again this should look for head.h in "folder", paste its content inside of main.c

Can't compile c program with RAWINPUTDEVICE

I wrote this simple main.c file, to test microsoft's Raw Input API.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
RAWINPUTDEVICE Rid[1];
int main() {
printf("Hello world!\n");
return 0;
}
But i can't compile it, i receive the following error:
gcc main.c
main.c:4:1: error: unknown type name 'RAWINPUTDEVICE'
RAWINPUTDEVICE Rid[1];
^
i'm using Code::Blocks, but i recieve the same error using Prompt.
I find just this post with an resembling error.
I also tryed to rename the main file to main.cpp and compile it with the g++ command, but i reciebed another error:
C:\Users\msouza\Desktop\Raw Input>g++ main.cpp
main.cpp:4:1: error: 'RAWINPUTDEVICE' does not name a type
RAWINPUTDEVICE Rid[1];
^

How do I compile with external header file in sublime text in C?

I use one header named header.h in main.c.
The function test is announced in header.h and defined in test.c.
However, it says words below even though I use build system as C.
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
Undefined symbols for architecture x86_64:
"test(int)", referenced from:
_main in main-a4d82e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.6s with exit code 1]
[cmd: ['bash', '-c', 'g++ -Wall -std=c++11 -O2 \'/Users/hanlock/Documents/CODE/TEST/TEST/main.c\' -o \'/Users/hanlock/Documents/CODE/TEST/TEST/main\' && osascript -e \'tell application "Terminal" to activate do script " cd \\"/Users/hanlock/Documents/CODE/TEST/TEST\\" &&start_ms=$(ruby -e \\"puts (Time.now.to_f * 1000).to_i\\")&&clear&&\\"/Users/hanlock/Documents/CODE/TEST/TEST/main\\" &&elapsed_ms=$(($(ruby -e \\"puts (Time.now.to_f * 1000).to_i\\") - start_ms))&& read -p \\"Press Enter to exit($elapsed_ms ms).\\"&&exit"\'']]
[dir: /Users/hanlock/Documents/CODE/TEST/TEST]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
I've read similar questions about this in C++. However, it seems not working in my case.
So, here comes the problem. How can I use external header file with sublime in language C?
Here is the code:
main.c
#include <stdio.h>
#include "./Header.h"
int main(int argc, const char * argv[]) {
test(1);
return 0;
}
header.h
#ifndef Header_h
#define Header_h
int test(int);
#endif
test.c
#include <stdio.h>
int test(int i){
printf("%d\n",i);
return 0;
}
In test.c:
#include "header.h"
In your shell:
gcc -I [dir] test.c
This will include any external header named header.h located in dir.
I hope this answers your question.

Resources