So I have this very simple program but I can't seem to get rid of a simple error.
I have a Header file with this
#ifndef FUNCTIONLOOKUP_H_INCLUDED
#define FUNCTIONLOOKUP_H_INCLUDED
enum functions
{
foo,
bar
};
//predefined function list
int lookUpFunction(enum functions);
#endif // FUNCTIONLOOKUP_H_INCLUDED
And in the src file i have the definition of lookUpFunction
Now when I call the lookUpFunction() from my main where I included the header file it gives me a undefined reference to it. The other awnsered questions where of no help.
#include <stdio.h>
#include <stdlib.h>
#include "FunctionLookUp.h"
int main()
{
lookUpFunction(foo); <---
return 0;
}
Function implementation
#include <stdio.h>
#include "FunctionLookUp.h"
typedef void (*FunctionCallback)(int);
FunctionCallback functionList[] = {&foo, &bar};
void foo(int i)
{
printf("foo: %d", i);
}
void bar(int i)
{
printf("bar: %d", i);
}
int lookUpFunction(enum functions)
{
int test = 2;
//check if function ID is valid
if( functions >= sizeof(functionList))
{
printf("Invalid function id"); // error handling
return 0;
}
//call function
functionList[functions](test);
return 1;
}
I can't seem to figure out where this error comes from.
You must have some file similar to:
/* FunctionLookUp.c */
#include "FunctionLookUp.h"
int lookUpFunction(enum functions)
{
/* code ... */
return x;
}
somewhere in order to solve your problem
You never show code that implements the function.
So it's most likely that what you're seeing is a linker error, the call itself is fine but the linker cannot find the code to call, so it throws an error.
Just declaring a function can't magically make it appear from somewhere, you must write the actual function too.
Related
Hello folks out there,
this is my code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "db_typedefs.h"
#include "operations.h"
int main(){
printf("Text\n");
int f = 3;
void add_mini(3);
}
operations.h
#ifndef ADD_OPERATIONS_H_INCLUDED
#define ADD_OPERATIONS_H_INCLUDED
void add_mini(int flag);
#endif // ADD_OPERATIONS_H_INCLUDED
operations.c
void add_mini(int flag)
{
int rc;
rc = flag;
printf("%i\n", rc);
}
Operations.c has also libraries included similar to main.c.
Compiler Error
error: expected declaration specifiers or '...' before numeric constant
regarding to void add_mini(3)
It seems like I'm unable to pass a simple integer value. While debugging it's even skipping the add_mini line.
Do you have any idea what's going on?
The whole code is embedded in a larger query to determine typed orders but this works fine. I just can't pass this simple integer value.
Thanks in advance.
When you use
void add_mini(3);
the compiler thinks it is a function declaration, not a function call. The argument 3 is not valid for a function declaration. Hence, the compiler complains.
Remove the void part to call the function.
int main(){
printf("Text\n");
int f = 3;
add_mini(3);
}
or, since you have initialized f to 3,
int main(){
printf("Text\n");
int f = 3;
add_mini(f);
}
Call the function like so: add_mini(3); rather than void add_mini(3);
Remove the word void for calling add_mini from main.c :
add_mini(3);
Or
(void)add_mini(3);
I have a simple program like:
int velocity=0;
#include "extra.h"
int main()
{
extra();
return 0;
}
where extra.h is:
void extra(){
velocity += 1;
}
yet when I compile this, I get the error:
extra.h:5:5: error: 'velocity' was not declared in this scope
Obviously, my code in extra.h can't "see" the variables in main.c, but why is this? How do I fix this?
You could add the following declaration to extra.h:
extern int velocity;
However, extra() shouldn't be defined in extra.h in the first place. This will cause problems if extra.h is included by multiple .c files in the same binary. The following is what you should have:
extra.h:
void extra();
extra.c:
#include "extra.h"
static int velocity = 0;
void extra() {
velocity += 1;
}
main.c:
#include "extra.h"
int main()
{
extra();
return 0;
}
I have created a small program mirroring my source code. Here main, when in debug mode, calls external library tester functions before it runs the main app. Imagine the constructor function in that library allocates memory and when in debug also tests some static functions. If that library is tested, it runs static tester code. If that library is used, static tester code. the static testers run every time the library function is called.
main.c
// calls test and library
#include <stdio.h>
#include <stdlib.h>
// to test if the lib is there and the function does what it says claims
extern int testExternalLibFunctions(void);
#include "lib.h"
int main(){
testExternalLibFunctions();
printf("main function uses doTheTango\n");
doTheTango();
// do the magic stuff here and
doTheTango();
return 0;
}
test_lib.c
#include <stdio.h>
#include "lib.h"
static int Static_doTheTangoTest();
int testExternalLibFunctions(){
// define DO_THE_TANGO_TEST_SELF_TRUE
Static_doTheTangoTest();
// undefine DO_THE_TANGO_TEST_SELF_TRUE
return 0;
}
int Static_doTheTangoTest(){
printf("external function tester calls doTheTango\n");
doTheTango();
return 0;
}
lib.h
#ifndef DO_THE_TANGO_HEADER
#define DO_THE_TANGO_HEADER
extern int doTheTango();
#endif // DO_THE_TANGO_HEADER
lib.c
#include <stdio.h>
#include <assert.h>
#include "lib.h" //self
// ONLY HERE SHOULD STATIC FUNCTIONS BE TESTED
static int STATIC_TEST();
int doTheTango(){
printf("Dancing is fun - ");
// if defined DO_THE_TANGO_TEST_SELF_TRUE
STATIC_TEST();
// endif
getchar();
return 0;
}
int STATIC_TEST(){
printf("Static test 1, Yet again!");
return 0;
}
It is not the intention to split tester and main function, because the main is calling more testers, etc ... they are inter-dependant!
How can I make the library do the static tests only when first included? Something like in python, where you test
if(__name__ == __main__) -> do the static tests
I'm not sure I understand what you are trying to do. I see from the source code that you say "Static test 1, Yet again!", so I assume you don't want the STATIC_TEST to be called on subsequent calls to doTheTango.
If this is what you want, then do:
int doTheTango(){
static int isTested = 0;
printf("Dancing is fun - ");
if (!isTested) {
isTested = 1;
STATIC_TEST();
}
getchar();
return 0;
}
This is working! and actually also what I was looking for!
// in the global doTheTangoTest you define:
extern int TEST_TANGO_SELF;
doTheTangoTest{
// to set it to 1 if the library is testing itself
TEST_TANGO_SELF = 1;
doTheTango();
// to set it to 0 if testing itself is finished
TEST_TANGO_SELF = 0;
}
and in doTheTango source
// you define again, but not as extern this time, just as int
int TEST_TANGO_SELF;
int doTheTango(){
printf("Dancing is fun - ");
// and use this global variable in a simple if statement
if(TEST_TANGO_SELF){
STATIC_TEST();
}
getchar();
return 0;
}
The other answers on these questions say to declare the function either in a header file or before main() but I have both of these and it still doesn't work.
#include <stdbool.h>
#ifndef WORK
#define WORK
#define TRACING true
#define DIMENSION 4
#define TOUR_LENGTH 15
void trace(char *s); //error here "Conflicting types for 'trace'"
#endif
^that is the header file
void trace(char *s) //error here "Conflicting types for 'trace'"
{
if (TRACING)
{
printf("%s\n",s);
}
}
^the function in question. the function is used multiple times in other .c files all with #include work.h
#include <stdbool.h>
#include "work.h"
#include "gameTree.h"
#include "gameState.h"
#include "stack.h"
#include <stdio.h>
/*
* trace
* Provide trace output.
* Pre-condition: none
* Post-condition: if trace output is desired then the given String
* parameter is shown on the console
* Informally: show the given message for tracing purposes
*
* param s the String to be displayed as the trace message
*/
void trace(char *s) //error here "Conflicting types for 'trace'"
{
if (TRACING)
{
printf("%s\n",s);
}
}
/*
* intro
* Provide introductory output.
* Pre-condition: none
* Post-condition: an introduction to the progrm has been displayed
* Informally: give the user some information about the program
*/
void intro()
{
printf("Knight's Tour\n");
printf("=============\n");
printf("Welcome to the Knight's Tour. This is played on a(n) %d x %d board. The\n",DIMENSION,DIMENSION);
printf("knight must move %d times without landing on the same square twice.\n",TOUR_LENGTH);
printf("\n");
}
/*
* main
* Program entry point.
* Pre-condition: none
* Post-condition: the solution to the Knight's Tour will be found
* and displayed
* Informally: solve the Knight's Tour
*/
int main(int argc, char *argv[])
{
gameTree g,a; // whole game tree and solution game tree
gameState s; // initial game state
stack k,r; // stack for intermediate DF use and for tracing solution
queue q; // queue for intermediate BF use
// give introduction
intro();
// initialise data structures
init_stack(&k);
init_queue(&q);
init_gameState(&s, 1, 1); // start at top left-hand corner: (1,1)
// show initial board
printf("\nStarting board:\n");
showGameState(s);
printf("\n");
// solve
init_gameTree(&g, false, s, 1);
a = buildGameDF(g, k, TOUR_LENGTH); // Depth-first
//a = buildGameBF(g, q, TOUR_LENGTH); // Breadth-first
// show results
if (isEmptyGT(a))
{
printf("No solution!\n");
}
else
{
// re-trace solution from leaf to root
init_stack(&r);
do
{
push(r, a);
a = getParent(a);
} while (!isEmptyGT(a));
// display move list
while (!isEmptyS(r))
{
a = (gameTree)top(r);
s = (gameState)getData(a);
printf("Move %d: (%d,%d)\n", getLevel(a), getRow(s), getColumn(s));
pop(r);
}
// display final path
printf("\nFinal board:\n");
showGameState(s);
}
^the whole main() and .c file, the errors occur mainly in the other supplementary files with the ADT definitions such as:
gameTree getParent(gameTree t)
{
gameTree p;
trace("getParent: getParent starts"); //error here "Conflicting types for 'trace'" and "Implicit declaration of function 'trace' is invalid in C99"
if (isEmptyGT(t))
{
fprintf(stderr, "getParent: empty game tree");
exit(1);
}
init_gameTree(&p, true, NULL, -1);
p->root = getTNParent(t->root);
trace("getParent: getParent ends"); //error here "Conflicting types for 'trace'" and "Implicit declaration of function 'trace' is invalid in C99"
return p;
}
^an example of where the trace function is called.
#include <stdbool.h>
#include "tNode.h"
#include "gameTree.h"
#include "work.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
^the imports for one of the ADT .c files
So yeah I don't know what to do.
To clarify work.h and work.c are the main files for this project.
void setChild(gameTree t, gameTree c)
{
trace("setChild: setChild starts"); //error here "Implicit declaration of function 'trace' is invalid in C99"
setTNChild(t->root, c->root);
trace("setChild: setChild ends"); //but not here
}
void setTNSibling(tNode t, tNode n)
{
trace("setTNSibling: setTNSibling starts"); //both errors here
t->sibling = n;
trace("setTNSibling: setTNSibling ends"); //only the "Implicit declaration of function 'trace' is invalid in C99" error here
}
I can't determine what is causing certain errors but if it were to do with importing incorrect header files shouldn't it be consistent all the way through?
I should also note that these files were given to me to use so I didn't write the original trace function.
It is not clear from your code samples whether <ncurses.h> is included or not in some of your source files. There is a function trace already defined in <ncurses.h> on MacOS:
/usr/include/curses.h:1710:extern NCURSES_EXPORT(void) trace (const unsigned int);
If this is the actual problem, you need to rename your trace function to something else.
From the context, you should probably use a macro TRACE(...) that expands to a call to fprintf(stderr, __VA_ARGS__) unless you compile for the release build in which case it would expand to nothing at all.
Another potential cause for warnings is the prototype:
void trace(char *s);
Passing a string constant for a char * argument may cause a warning. Since trace does not modify the contents of the string argument, it should be declared const char *. Fix this by modifying the prototype:
void trace(const char *s);
I am trying to learn creating header file in C and including it in my main.c func() . I created a simple tut1.c file with function named call() and a tut1.h header file which externs tut1.c function named call(). Thats it, now i am using eclipse Juno for C/C++ on linux fedora. I dont get any compile error but the code wont output? I tried on console and eclipse in vain. Can you check please? Thanks
---main.c-----
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "tut1.h"
int main (void)
{
int tut1(void);
return 0;
}
-----tut1.c------
#include <stdio.h>
#include <stdlib.h>
#include "tut1.h"
int call (void)
{
int *ptr;
int i;
ptr = &i;
*ptr = 10;
printf ("%d we are printing the value of &i\n", &i);
printf ("%d we are printing the value of *ptr\n", *ptr);
printf ("%d we are printing the value of ptr\n", ptr);
printf ("%d we are printing the value of &ptr\n", &ptr);
getchar();
return 0;
}
----tut1.h----
#ifndef TUT1_H_
#define TUT1_H_
extern int call (void);
#endif
You're not seeing anything because you're not calling the call() function from your main() function.
The main() function is the default entry point when you run the program, i.e. the first function that gets called during execution.
To execute the function call() you would need to call this from main() as follows :
int main (void)
{
int result = call();
return 0;
}
BTW, this line int tut1(void); within your main() just declares a function, which you do not seem to have defined anywhere. So I have removed it in the above shown code.