I can't figure why the following code gives compilation error as if the typedef definition not found. In fact, if I add the line 'typedef TCHAR my_tchar;' (which is already in win32def.h) in app.h the compilation goes well.
win32def.h
#ifndef win32def_h
#define win32def_h
#include <tchar.h>
typedef TCHAR my_tchar;
#endif
app.h
#include "win32def.h"
int my_function(const my_tchar *filename, ....)
compilation error at line of my_function:
error C2143: syntax error : missing ')' before '*'
what the compiler is used?
I'm not sure, but try to turn on the option "treat wchar_t as built-in type".
Related
I am trying to move some functions to separate file in c project.
I made util.h file with
#ifndef _UTIL_H
#define _UTIL_H
#include <signal.h>
#include <termios.h>
#include <time.h>
...
extern struct timeval tv1, tv2, dtv;
void time_start();
long time_stop();
and I made util.c file with
#include "util.h"
...
struct timeval tv1, tv2, dtv;
void time_start() { gettimeofday(&tv1, &timezone); }
long time_stop()
{
gettimeofday(&tv2, &timezone);
dtv.tv_sec = tv2.tv_sec - tv1.tv_sec;
...
in cmake I have
add_executable(mpptd mpptd.c util.c)
and I get the following errors during compile
[build] ../settings/daemons/util.c: In function ‘time_stop’:
[build] ../settings/daemons/util.c:14:8: error: invalid use of undefined type ‘struct timeval’
[build] dtv.tv_sec = tv2.tv_sec - tv1.tv_sec;
[build] ^
and
[build] ../settings/daemons/util.c: At top level:
[build] ../settings/daemons/util.c:7:16: error: storage size of ‘tv1’ isn’t known
[build] struct timeval tv1, tv2, dtv;
[build] ^~~
What can be wrong here? Why "storage size" error goes later than "undefined type" error? Whouldn't it go earlier?
struct timeval is defined in sys/time.h. You'll need to include that.
#ifndef _UTIL_H
#define _UTIL_H
#include <signal.h>
#include <termios.h>
#include <time.h>
#include <sys/time.h>
...
The struct definition (the part with struct { .. }) must be visible to code using that struct. Simply put that part in a header visible to the code using the struct, then include that header.
Otherwise the struct ends up as a forward declaration, of incomplete type. Incomplete types don't have any size, hence the cryptic errors.
In this case you seem to be using some non-standard, non-POSIX (or possibly obsolete POSIX?) library. Strict compilers (like gcc in -std=c11 -pedantic-errors mode) won't allow non-standard crap in standard headers, so you'll have to include the specific non-standard header separately, the struct won't be found in time.h.
This is a silly compilation error, but for the life of me I can't find out what's wrong. I have spent hours on it, but I have made no progress at all. And I certainly don't understand enough about OpenSSL to understand what an X509_NAME is.
I'm compiling a small C file whose function it is to print out an error message if anyone calls one of the 30 or so functions in the file, to indicate that the 30 SSL functions are not supported by the C program. Each function is about 3 lines long (see below). The calling interface is copied from the OpenSSL openssl/X509.h file for win32. (I'm running Windows10 x64, but compiling with VStudio 2017 command line set up for 32bits).
Here is the original interface from the openssl/x509.h include file:
int X509_NAME_get_text_by_NID(X509_NAME *name, int nid, char *buf, int len);
Since the original x509.h include file says inside that "the definition for X509_NAME on Win32 is located in wincrypt.h," I copied the definition for the X509_NAME macro directly into my source file as shown below.
The code for my simple error message function follows. I just copied the interface definition from the openssl/x509.h file and made it into a function:
#define X509_NAME ((LPCSTR) 7) /* copied from wincrypt.h */
int X509_NAME_get_text_by_NID(X509_NAME *name, int nid, char *buf, int len) {
print_error (E_NO_SSL_SUPPORT);
return (-1);
}
When I try to compile the code with the VS 2017 command line compiler for 32bits, I get these error messages. Line 236 contains the troublesome code line int X509_NAME_....
cl /c /Od /nologo /MT /I. "-I..\s" ..\s\stubssl.c
stubssl.c
..\s\stubssl.c(236): error C2143: syntax error: missing ')' before '('
..\s\stubssl.c(236): error C2091: function returns function
..\s\stubssl.c(236): error C2059: syntax error: ')'
..\s\stubssl.c(236): error C2143: syntax error: missing ')' before 'constant'
..\s\stubssl.c(236): error C2143: syntax error: missing '{' before 'constant'
..\s\stubssl.c(236): error C2059: syntax error: 'constant'
..\s\stubssl.c(242): error C2059: syntax error: '<parameter-list>'
c:\stubssl\winxp\make.exe: *** [stubssl.obj] Error 2
I get the same kind of error with another function that is a bit simpler (but that also uses the X509_NAME macro). It starts on line 242 (see the error messages above).
X509_NAME *X509_get_subject_name(X509 *a) {
hio_oerror (E_NO_SSL_SUPPORT);
return (NULL);
}
Does anyone have any idea how I might try to resolve this problem? It seems so simple, yet it has been very elusive for me to solve. I can't imagine how I can be missing ')' before '(' in those lines.
EDIT: My include files in stubssl.c:
#define WIN32_LEAN_AND_MEAN
#include "openssl/ssl.h"
#include "openssl/x509.h"
#include "openssl/bio.h"
#include "openssl/pem.h"
In openSSL, X509_NAME is a type, but <wincrypt.h> defines it as a value (((LPCSTR) 7)). Don't define it yourself, but use the openSSL header, don't include <wincrypt.h> and #define WIN32_LEAN_AND_MEAN to avoid it being included through other windows headers.
Below is the code of my header file trie.h.
The compiler keep showing the following error:
In file included from speller.c:11:
./trie.h:3:8: error: redefinition of 'letter'
struct letter
^
./trie.h:3:8: note: previous definition is here
struct letter
^
1 error generated.
make: *** [speller.o] Error 1
The code:
struct letter
{
int is_word;
struct letter* arr[27];
};
// fuctions
struct letter* create_trie();
void free_trie(struct letter* trie);
Most likely your file gets included multiple times, hence the redefinition error.
To avoid this problem use include guards:
#ifndef HEADERNAME_DEFINED
#define HEADERNAME_DEFINED
// your code goes here.
#endif // HEADERNAME_DEFINED
or you can use non-standard preprocessor directive like #pragma once to do the job. It results in less code, and sometimes faster compilation speed.
Put that on top of your file:
#pragma once
// your code goes here
Note: The comment (// HEADERNAME_DEFINED part) after #endif isn't necessary. It is just a hint for programmer to know what belongs together.
As part of getting familiar with GObject I'm trying to create a "Hello, world" interface following the example in the reference manual. Here's what I have in hello_world_if.h:
#ifndef __HELLO_WORLD_IF_H__
#define __HELLO_WORLD_IF_H__
#include <glib-object.h>
G_BEGIN_DECLS
#define TYPE_HELLO_WORLD_IF (hello_world_if_get_type())
#define HELLO_WORLD_IF(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_HELLO_WORLD_IF, HelloWorldIf))
#define IS_HELLO_WORLD_IF(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_HELLO_WORLD_IF))
#define HELLO_WORLD_IF_GET_INTERFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE((inst), TYPE_HELLO_WORLD_IF, HelloWorldIfInterface))
typedef struct _HelloWorldIf HelloWorldIf; /* dummy object */
typedef struct _HelloWorldIfInterface HelloWorldIfInterface;
struct _HelloWorldIfInterface {
GTypeInterface parent;
gchar *(*get_hello)(HelloWorldIf *self);
};
GType hello_world_if_get_type(void);
gchar *hello_world_if_get_hello(HelloWorldIf *self);
G_END_DECLS
#endif /* __HELLO_WORLD_IF_H__ */
and in hello_world_if.c:
#include "hello_world_if.h"
G_DEFINE_INTERFACE(HelloWorldIf, hello_world_if, 0);
static void
hello_world_if_default_init(gpointer g_class) {
/* Add properties and signals to the interface here */
}
gchar *
hello_world_if_get_hello(HelloWorldIf *self) {
g_return_if_fail(IS_HELLO_WORLD_IF(self));
HELLO_WORLD_IF_GET_INTERFACE(self)->get_hello(self);
}
But this doesn't compile:
$ make
gcc -g -pthread -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -c -o hello_world_if.o hello_world_if.c
hello_world_if.c:3: error: expected declaration specifiers or ‘...’ before ‘hello_world_if’
hello_world_if.c:3: error: expected declaration specifiers or ‘...’ before numeric constant
hello_world_if.c:3: warning: data definition has no type or storage class
make: *** [hello_world_if.o] Error 1
From reading other answers here it seems this "expected declaration specifiers" message often means a necessary header file hasn't been included or has been included too late. But I'm not sure how that could be the case here. (Specifically, adding #include <glib.h> or #include <glib-object.h> to the C file doesn't change anything.)
I must be missing something simple but I just don't see it. Help?
Turns out there's a simple explanation: The G_DEFINE_INTERFACE macro was added in GLib 2.24.0, but I'm using version 2.22.5 (the standard on CentOS 6.3). I'll need to either build and install a newer version of GLib or dig up older reference documentation—the website doesn't go back further than 2.26.1.
Hello i have the following code..
#define VB_CHAR signed char
#define VB_UCHAR unsigned char
#if defined(VBISAM_NO_CISAM_CONFLICT)
#include "vbisam_rename.h"
#endif
#include "vbdecimal.h"
vbdecimal doesnt include any other header but fails to see it
Error 1 error C2065: 'VB_CHAR' : undeclared identifier c:\users\parhs\documents\visual studio 2010\projects\testdll4\vbdecimal.h
I have to define VB_CHAR in vbdecimal in order to work..
Any work-around for this?
Well. It could be that vbisam_rename.h '#undefs' VB_CHAR.