There is a header file in my project that contains this line:
typedef unsigned short uint16_t;
and in MinGW compiler , there is a file "stdint.h" that also has the same line:
typedef unsigned short uint16_t;
When I compile, I get the following error:
error (dcc:1086):redeclaration of uint16_t
Can anyone please explain this to me .... I mean they are both unsigned short ..
uint16_t is already defined in stdint.h as an unsigned 16 bit integer type. You should just omit your typedef.
Related
I am working with a C code base that uses an API function (https://developer.gnome.org/glib/stable/glib-Hash-Tables.html#g-hash-table-size) that returns a guint. I needed to pass this along downstream as an int. I am wondering if there's any way to do this?
I searched the docs and Google but came up empty.
The mere existence of these typedefs baffles me. Check glib/gtypes.h:
typedef char gchar;
typedef short gshort;
typedef long glong;
typedef int gint;
typedef gint gboolean;
typedef unsigned char guchar;
typedef unsigned short gushort;
typedef unsigned long gulong;
typedef unsigned int guint;
So no conversion is needed between unsigned int and guint, they are the same type.
The usual warnings about converting between unsigned int and int apply.
#include<stdio.h>
#include<stdlib.h>
#include <dir.h>
#include <dos.h>
#include <dirent.h>
struct ffblk ffblk;
int main()
{
printf("Successfully made ffblk");
}
At the moment, all I'm trying to do is make the structure ffblk. Any suggestions?
struct ffblk {
char lfn_magic[6] __attribute__((packed));
short lfn_handle __attribute__((packed));
unsigned short lfn_ctime __attribute__((packed));
unsigned short lfn_cdate __attribute__((packed));
unsigned short lfn_atime __attribute__((packed));
unsigned short lfn_adate __attribute__((packed));
char _ff_reserved[5] __attribute__((packed));
unsigned char ff_attrib __attribute__((packed));
unsigned short ff_ftime __attribute__((packed));
unsigned short ff_fdate __attribute__((packed));
unsigned long ff_fsize __attribute__((packed));
char ff_name[260] __attribute__((packed));
};
This is a copy of struct ffblk from an old dir.h file - meant for dos I believe.
I do not know what you are doing but you really should find an appropriate version of this object for your system. Your code might possibly compile using this definition of struct ffblk, but I would doubt its integrity. It appears that your system's dir.h file does know about this struct. Try editing the dos.h and dir.h files to see what is going on.
For example, you may need to define something to get the struct included correctly. Sometimes changing the order of header files in program code can clear up this kind of problem.
I have a portable definition of types in a header of a project that i aim to compile on multiple platforms. I'm using the following typedefs:
#ifndef PLATFORM_H
#define PLATFORM_H
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#ifndef TRUE
#define TRUE (1)
#endif
#ifndef FALSE
#define FALSE (0)
#endif
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed int int16_t;
typedef unsigned int uint16_t;
typedef signed long int int32_t;
typedef unsigned long int uint32_t;
typedef signed long long int int64_t;
typedef unsigned long long int uint64_t;
#endif
This header is platform-specific, so i have a similar declaration for every platform, making sure that integers are always the same. However, in compiling, i get this:
conflicting types for ‘int32_t’
In file included from /usr/include/stdlib.h:314:0,
from platform.h:5,
from main.c:1:
/usr/include/x86_64-linux-gnu/sys/types.h:196:1: note: previous declaration of ‘int32_t’ was here
And other errors. What might be the cause? Can i override a typedef, or check for it's existence first?
You get an error because you picked names for your typedefs that collide with names of types from the <stdint.h> header.
Since you are looking for types that are already defined in stdint.h, you might as well include it on platforms that provide this header.
It is my first experience with cygwin gcc, before that I used it on linux.
I faced a problem solution for which I failed to find in net.
I want to compile C source file and have included this source
// Value type defenitions
// --- chars --- //
typedef unsigned char UChar;
typedef char Char;
// --- short int --- //
typedef unsigned short UShort;
typedef short Short;
// --- int --- //
typedef unsigned int UInt;
typedef int Int;
// --- long int --- //
typedef long Long; // 32 bits length
typedef unsigned long ULong; // unsigned 32 bits length
// --- long long int --- //
typedef unsigned long long UBig; // 64-bit length unsigned
typedef long long Big; // 64-bit length
// --- decimals --- //
typedef float Float;
typedef double Double;
typedef long double Triple; // 80-bit length. Actual properties unspecified.
and have got this error
Include/null.h:6: error: redefinition of typedef 'UChar'
Include/null.h:6: error: previous declaration of 'UChar' was here
Include/null.h:7: error: redefinition of typedef 'Char'
Include/null.h:7: error: previous declaration of 'Char' was here
Include/null.h:9: error: redefinition of typedef 'UShort'
Include/null.h:9: error: previous declaration of 'UShort' was here
and so on...
Thank you for help!
Looks like you have included the same header, where you have typedef'edthese, more than once. Use include guards to avoid multiple inclusion.
I'm getting loads of errors like these:
gfx.h:48: error: syntax error before 'buffer'
gfx.h:48: warning: type defaults to 'int' in declaration of 'buffer'
gfx.h:48: warning: data definition has no type or storage class
gfx.h:73: error: syntax error before 'uint16_t'
gfx.h:73: warning: no semicolon at end of struct or union
gfx.h:74: warning: type defaults to 'int' in declaration of 'visible_lines_per_frame'
gfx.h:74: warning: data definition has no type or storage class
...
I'm a bit tired, so I can't figure out what could be causing these.
This is the definition of buffer (starting at line 43, going to line 57):
/* 8-bit architecture (not yet used.) */
#if PROC_BIT_SIZE == 8
uint8_t buffer[GFX_SIZE];
# define GFX_PIXEL_ADDR(x,y) (x / 8) + (y * (GFX_WIDTH / 8))
/* 16-bit architecture: dsPIC */
#elif PROC_BIT_SIZE == 16
uint16_t buffer[GFX_SIZE / 2];
# define GFX_PIXEL_ADDR(x,y) (x / 16) + (y * (GFX_WIDTH / 16))
/* 32-bit architecture: AVR32(?), STM32 */
#elif PROC_BIT_SIZE == 32
uint32_t buffer[GFX_SIZE / 4];
# define GFX_PIXEL_ADDR(x,y) (x / 32) + (y * (GFX_WIDTH / 32))
/* Other, unknown bit size.*/
#else
# error "processor bit size not supported"
#endif
(It's designed to support multiple architectures 8-bit MCUs to 32-bit MCUs.)
I've defined uint8_t etc. because the GCC I'm using doesn't seem to have a stdint.h header.
Here is how I have defined uint8_t etc.
/*
* stdint.h support.
* If your compiler has stdint.h, uncomment HAS_STDINT.
*/
//#define HAS_STDINT
#ifndef HAS_STDINT
// D'oh, compiler doesn't support STDINT, so create our own,
// 'standard' integers.
# if PROC_BIT_SIZE == 8 || PROC_BIT_SIZE == 16
typedef char int8_t;
typedef int int16_t;
typedef long int32_t;
typedef long long int64_t;
typedef unsigned char uint8_t;
typedef unsigned int uint16_t;
typedef unsigned long uint32_t;
typedef unsigned long long uint64_t;
# elif PROC_BIT_SIZE == 32
typedef char int8_t;
typedef short int16_t;
typedef int int32_t; // usually int is 32 bits on 32 bit processors, but this may need checking
typedef long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long uint64_t;
# endif
#else
# include <stdint.h>
#endif
The warning: no semicolon at end of struct or union implies that you've left a semicolon off at the end of a struct or union defined earlier in the same file, or in another header that was included earlier. This would result in malformed statements such as:
struct S { ... } uint8_t buffer[GFX_SIZE];
I apparently have solved my problem.
My mistake was to not declare PROC_BIT_SIZE before the "typedef" definitions of uint8_t and friends. The preprocessor therefore ignored the declarations, causing the error.
Just a blunder of my own making.