I am having trouble spotting where I am making a mistake and not sure how to google the solution. I am getting the following error:
In file included from buttons.h:8,
from buttons.c:1:
debug_mode.h:14: error: expected ')' before 'Button'
I have an enum declared in buttons.h
#ifndef BUTTONS_HEADER
#define BUTTONS_HEADER
#include <avr/io.h>
#include <stdbool.h>
#include <util/delay.h>
#include "uart.h"
#include "debug_mode.h"
typedef enum {
NO_BUTTON,
BUTTON1,
BUTTON2,
BUTTON3,
BUTTON4,
BUTTON5,
BUTTON6
}
ButtonFlags;
void CheckButtons();
void SetButtonFlag();
void ProcessButtons();
#endif
I am including it in another header debug_mode.h:
#ifndef DEBUG_MODE_HEADER
#define DEBUG_MODE_HEADER
#include "uart.h"
#include <stdbool.h>
#include <avr/pgmspace.h>
#include "buttons.h"
bool DebugModeEnabled = false;
void SetDebugMode();
void AnnounceDebugMode(bool State);
void DebugAnnounceLEDState();
void DebugAnnounceButtonState(ButtonFlags Button);
#endif
and the debug_mode.c:
#include "debug_mode.h"
void DebugAnnounceButtonState(ButtonFlags Button)
{
SendUARTString_P(DEBUGMODE_BUTTON_PRESSED_MSG);
switch (Button)
{
case 1: SendUARTString_P(DEBUGMODE_BUTTON1_MSG); break;
default: break;
}
}
Any assistance would be appreciated
Your headers buttons.h and debug_mode.h are including each other. You will need to refactor your code in such a way to remove this circular dependency.
Related
My compilation order is :
core1.c
top.c
core1.c contents :
#include "header1.h"
#include "header2.h"
void function1() {
---- }
void function2() {
---- }
header1.c contents function declarations, enums, includes :
#include comdef.h
void function1();
void function2();
top.c contents :
#include "header1.h"
#include "header2.h"
void main() {
function1();
function2();
}
I will add more headers and more core C files into my project. Each core.c file needs the same header files. How to get this all working, without the need to put #include header1/2.h in each core1.c, core2.c etc, and include these headers only in main.c ?
You can use a global header including all files
/* glob.h */
#ifndef GLOB_H
#define GLOB_H
#include "header1.h"
#include "header2.h"
#endif /* GLOB_H */
and in your main file
#include "glob.h"
Even if this is considered bad style, there are several projects using this approach, i.e. gtk
Use one header for each source file:
core1.h:
#ifndef _CORE1
#define _CORE1
#include comdef.h
void function1();
void function2();
#endif
core1.c:
#include "core1.h"
void function1() {
---- }
void function2() {
---- }
top.c:
#include "core1.h"
void main() {
function1();
function2();
}
I plan to use both SDL_GetKeyboardState and SDL_Overlay but it seems there is a conflict.
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL2/SDL.h>
int main()
{
const Uint8 *keystate = SDL_GetKeyboardState(NULL);
SDL_Overlay *bmp;
printf("hello world!");
}
Compile:
gcc -c main.cpp
When order of headers are:
#include <SDL/SDL.h>
#include <SDL2/SDL.h>
error: ‘SDL_GetKeyboardState’ was not declared in this scope
const Uint8 *keystate = SDL_GetKeyboardState(NULL);
^
or
#include <SDL2/SDL.h>
#include <SDL/SDL.h>
error: ‘SDL_Overlay’ was not declared in this scope
SDL_Overlay *bmp;
^
Even adding
#include <SDL2/SDL_video.h>
does not solve the problem.
What header should I add to use SDL_Overlay?
I'm having trouble finding my error. Here is a definition in structures.h
typedef struct book {
bank_account_t **accounts;
transaction_t **transactions;
} book_t;
And here is in functions.c where I include header and try to use the type book_t
#include "structures.h"
void load_book(book_t *book) {
}
But I get this error
functions.c:10:16: error: unknown type name ‘book_t’
void load_book(book_t *book) {
^
Edits with more code below:
In my main file I order my .h files like so
#include "structures.h"
#include "functions.h"
structures.h
#ifndef STRUCTURES_H
# define STRUCTURES_H
typedef struct bank_account {
char *name;
int amount;
} bank_account_t;
typedef struct transaction {
char *name;
int amount;
} transaction_t;
typedef struct book {
bank_account_t **accounts;
transaction_t **transactions;
} book_t;
#endif
function.c
#include <stdio.h>
#include "functions.h"
#include "structures.h"
#include "bank_account.h"
#include "transaction.h"
void load_book(book_t *book) {
}
void init_book() {
}
bank_account.h
#ifndef BANK_ACCOUNT_H
# define BANK_ACCOUNT_H
void init_new_bank();
void deinit_new_bank();
#endif
transaction.h
#ifndef TRANSACTION_H
# define TRANSACTION_H
#endif
I think the problem must be in functions.h (which is not included in the original post).
functions.h
#ifndef FUNCTIONS_H
# define FUNCTIONS_H
/* [MarkU] required: include definition of book_t */
#include "structures.h"
void load_book(book_t *book);
void init_book();
#endif
Without the #include structures.h there is no definition of the boot_t type.
Built and verified with mingw32-gcc 4.7.2. Omitting the #include, I see the error message.
In functions.c change the order of those:
#include "functions.h"
#include "structures.h"
to be
#include "structures.h"
#include "functions.h"
The subtile thing is that the error message origins from functions.c not from functions.h.
Assuming the protoytpe to load_book(book_t *) in functions.h, it needs to know about book_t.
So the optimal solution to this would be to include structures.h into functions.h (as also already pointed out by MarkU's answer).
Lesson learned: Always (and only) include what you need and where you need it. Avoid (subtile) dependencies.
I am experiencing this error at my preprocessText() function (below) in my .c and I'm not entirely sure why. From browsing it seems most people were missing a { or ( or ; etc somewhere, but I'm fairly certain I am not.
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "Assembler.h"
int main(int argc, char** argv) {
// ...
preprocessText(file, inter1);
// ...
}
public void preprocessText(FILE* file, FILE* file2) { //error happens at this declaration
// ...
}
My header file is:
#ifndef ASSEMBLER_H
#define ASSEMBLER_H
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stddef.h>
// ...
void preprocessText(FILE* file, FILE* file2);
#endif
All methods are implicitly accessible by any other piece of code, if the function name is in scope. There is no public keyword in c
You have 'public' before 'void'. Remember, this is C ;)
a cpp file:
#include <iostream>
#include <jni.h>
#include "Hello.h"
#include "windows.h"
#include "stdafx.h"
typedef void(__stdcall *Print_)();
int main(){
HINSTANCE hDll; //DLL句柄
Print_ print_; //函数指针
hDll = LoadLibrary("Hello.dll");
if (hDll != NULL)
{
print_ = (Print_)GetProcAddress(hDll,"Java_Hello_sayHello#8");
if(print_!=NULL)
{
print_();
}
FreeLibrary(hDll);
}
return 0;
}
//there is something wrong, it prints:
http://i983.photobucket.com/albums/ae311/keatingWang/c_wrong.png
未声明的标识符 means : Undeclared identifier
Consider the macro:
#define HINSTANCE "hDll"
and its use:
HINSTANCE hDll; //DLL句柄
after preprocessing it would look like:
"hDll" hDll;
which clearly is an error as it makes hDll undeclared as "hDll" is not a valid type.
Could it be a pre-compiled header issue? With some project settings VC++ will skip stuff before the #include "stdafx.h", which I think might be the cause of the C4627 warnings you're getting. Have you tried moving #include "stdafx.h" before your other #includes?
remove
#define HINSTANCE "hDLL"
To remove C4627 warning, move up #include "stdafx.h" to the top (to be the first #include) as indicated by Mike Dinsdale's answer. This will probably solve error for LoadLibrary, GetProcAddress, and FreeLibrary:
#include "stdafx.h" // moved up
#include <iostream>
#include <jni.h>
#include "Hello.h"
#include "windows.h"