I can't find what is wrong with my code in C:
error.h
#ifndef ERROR_H_INCLUDED
#define ERROR_H_INCLUDED
void myfunc(bool**,int); //error line 1
#endif
Here is the function declaration:
error.c
#include "error.h"
void myfunc(bool **rel,int num){ //error line 2
//function code here
}
The call of the function is :
main.c
#include "error.h"
int main(){
bool **rel;
int num;
myfunc(rel,num);
return 0;
}
The above code returns the error
expected ')' before '*' token
at the error line 1 and error line 2.I put the function code in comments and i still have this error.I know that, that kind of error is a missing ; or ) at the most times but i spend hours and didn't find the error.
bool type is not recognized, you need to include stdbool.h in your error.h header.
Related
While compiling the C code as follows:
#include <stdio.h> // Notice the library included in the header of this file
#include <stdlib.h>
#include "myLibrary.h" // Notice that myLibrary.h uses different include syntax
#define MAX_LENGTH 21.8
#define WORK_WEEK 5
int main(void) {
function1();
return EXIT_SUCCESS;
}
I am getting the following:
d:/programs/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\svtte\AppData\Local\Temp\ccyHWfzC.o:02_01.c:(.text+0xc): undefined reference to `function1'
collect2.exe: error: ld returned 1 exit status
The myLibrary.h file is as follows:
#ifndef MYLIBRARY_H_
#define MYLIBRARY_H_
void function1(void);
void function2(void);
#endif /* MYLIBRARY_H_ */
and myLibrary.c is as follows:
void function1(void){
puts("It works :)");
}
void function2(void){
//This function does nothing as well
}
Any reasons as to why I am getting the error response would be helpful. Also, any pointers to the possible fixes would be great.
The likelihood is that you’re not including 'myLibrary' when you link to build the executable.
When I try to compile my code, I'm getting the error:
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_init (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_lock (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_unlock (referred from lab4_t0.o).
I read that "Undefined symbol errors can occur when a function is declared (as is the case for the lock functions in lock.h), but it is not properly implemented." But I feel that I am implementing all the functions in lock.h properly since all the functions are of type voidand I don't return anything. So I don't know what I could be doing wrong to get this error.
Any help would greatly be appreciated!
Note: Please let me know if I need to provide more code. I don't think I need to write what I put in the function implementations of lock.h since I feel all you need to know is that the function implementations do not return anything, but please let me know if I need to include that.
Code
lock.c
#include "lock.h"
extern process_t * current_process;
extern process_t * process_queue;
void l_init(lock_t* l){
//Does stuff but never type return or return anything
}
void l_lock(lock_t* l){
//Does stuff but never type return or return anything
}
void l_unlock(lock_t* l){
//Does stuff but never type return or return anything
}
lock.h
#ifndef __LOCK_H_INCLUDED__
#define __LOCK_H_INCLUDED__
#include "3140_concur.h"
#include "shared_structs.h"
void l_init(lock_t* l);
void l_lock(lock_t* l);
void l_unlock(lock_t* l);
#endif /* __LOCK_H_INCLUDED */
3140_concur.c
#include "3140_concur.h"
#include <stdlib.h>
3140_concur.h
#ifndef __3140_CONCUR_H__
#define __3140_CONCUR_H__
#include <stdlib.h>
#include <fsl_device_registers.h>
#include "process.h"
void process_blocked (void);
void process_terminated (void);
unsigned int * process_stack_init (void (*f)(void), int n);
void process_stack_free (unsigned int *sp, int n);
void process_begin (void);
#endif
process.h
#include <stdlib.h>
#include <fsl_device_registers.h>
#include "3140_concur.h"
struct process_state;
typedef struct process_state process_t;
unsigned int * process_select (unsigned int * cursp);
extern process_t * current_process;
extern process_t * process_queue;
void process_start (void);
int process_create (void (*f)(void), int n);
process.c
(I do process_t* process_queue = NULL; & process_t* current_process = NULL; b/c I want them to be NULL before any functions are called)
#include "3140_concur.h"
#include "shared_structs.h"
#include <stdlib.h>
#include <fsl_device_registers.h>
process_t* process_queue = NULL;
process_t* current_process = NULL;
lab4_t0.c
#include "process.h"
#include "utils.h"
#include "lock.h"
lock_t l;
\\uses functions in lock.h
Update
Build output
*** Using Compiler 'V5.06 update 4 (build 422)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin'
Build target 'Target 1'
compiling 3140_concur.c...
compiling lab4_t0.c...
lab4_t0.c(42): warning: #111-D: statement is unreachable
return 0;
lab4_t0.c: 1 warning, 0 errors
compiling process.c...
process.c(100): warning: #1-D: last line of file ends without a newline
}
process.c: 1 warning, 0 errors
linking...
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_init (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_lock (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_unlock (referred from lab4_t0.o).
Not enough information to list image symbols.
Finished: 1 information, 0 warning and 3 error messages.
".\Objects\Lab4.axf" - 3 Error(s), 2 Warning(s).
Target not created.
Build Time Elapsed: 00:00:39
Thanks to #AjayBrahmakshatriya, it turns out I didn't add lock.c to my project. That fixed everything. Whew.
So whenever I try to run my Makefile on my server, it always gives me the error is "Memory.c: 9 error: expected ')' before '*' token. But when I try to run it on my own computer, it works just fine. I've been trying to figure out what is wrong but can't seem to find it.
I've attached the 3 files that are used in this part of my program. Memory.c, Memory.h and ProcessInput.h.
This is Memory.c
/* Initializes memory */
#include <stdio.h>
#include <stdlib.h>
#include "memory.h"
void initializeMemory(memory** memArray, int memSize)
{
// Allocating space for memory array
*memArray = malloc(memSize * sizeof(memory));
if(*memArray == NULL)
{
fprintf(stderr, "Error allocating space for array of memory" );
exit(1); // exit(1) = Unsuccessful exit
}
// Initializing the contents within memory array
int i = 0;
for(i = 0; i < memSize; i ++)
{
((*memArray)[i]).occupied = false;
}
}
and this is Memory.h
// Definitions for Memory.c
#define bool int
#define true 1
#define false 0
#include "ProcessInput.h"
// Include guards to prevent redefinition of struct
#ifndef MEMORY_H
#define MEMORY_H
typedef struct memoryDetail
{
process process;
bool occupied;
} memory;
#endif
// Function declaration for memory.c
void initializeMemory(memory** memArray, int memSize);
the only thing used from ProcessInput.h is the process structure defined in ProcessInput.h
This is ProcessInput.h
// Include guards to prevent redefinition of struct
#ifndef PROCESSDETAIL_H
#define PROCESSDETAIL_H
typedef struct processDetail
{
int timeCreated;
int processID;
int memorySize;
int jobTime;
} process;
#endif
// function declarations for ProcessInput.c
void processInput(int* maxSize, int* count, process** processes, char* fileName);
I'm not too sure why it's giving me the error. I don't know where I'm supposed to be putting a missing right brace. Any advice is much appreciated!
edit: As informed, these are the following questions that I looked at but to not avail.
error: expected ‘)’ before ‘*’ token
Multiple of same error while compiling "error: expected ')' before '*' token
http://www.dreamincode.net/forums/topic/288956-error-expected-before-token/
thanks everyone for the help!
#include "memory.h" is different to #include "Memory.h" (i.e. C is case sensitive)
If you tried #include "myfile.h" instead of #include "MyFile.h" the error may be more obvious. In this case it just happens that the compiler finds the system memory.h.
<memory.h> is a header from C library of pre-standard era. It is quite possible that your standard library still provides it and the compiler takes that one instead of yours.
Try renaming your header file and see if it changes anything.
I have a simple log macro, this is the .h file :
#ifndef __LOGGER_H
#define __LOGGER_H
#define LOG_ERROR(errorMsg) logError(__FILE__, __LINE__, errorMsg)
#endif
...and this is the .c file :
#include <stdio.h>
#include "Logger.h"
void logError(const char* filename, int line, const char* errorMsg)
{
printf("[File : %s] - [Line: %d} - [Error Message : %s]\n", filename, line, errorMsg);
}
When I use the macro in a .c file :
#include <Common/Logger.h>
void func(int index, int value)
{
if (IsIndexOutOfRange(index, NUMBER_OF_PARAMS))
{
LOG_ERROR("Index out of range!");
return;
}
.
.
.
}
...and build (vs2013) I get :
warning C4013: 'logError' undefined; assuming extern returning int
Warning C4013 usually means that you forgot to include the correct library or need to use extern. I suspect I need extern somehow, but do not know how to fix this with a macro.
How do I solve this?
i'm working since a few weeks on the same project and never had this kind or error.
I got it now without touching to the concerned file, which is down there:
#ifndef DIJKSTRA_H_INCLUDED
#define DIJKSTRA_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INFINI 100000.0
typedef struct dij
{
int ordre;
float** adjacencePoids;
float* l;
int* pred;
}t_dij;
int choix_action();
int choix_sommet_depart();
int choix_sommet_arrivee();
t_dij* allouer_dijkstra();
t_dij* allouer_dijktra_durees();
t_dij* dijkstra();
void afficher_resultat();
void sauver_resultat();
void detruire_struc();
#endif // DIJKSTRA_H_INCLUDED
This code gives me this error (Compiler is MinGW)
Line 11 error: expected identifier or '(' before 'typedef'|
Thanks!
I suspect the error lies in the file the header shown is included from, or in a file included just before this one.