[I've been looking at other questions and none of the solutions have worked so I'll ask my own question.
I'm working on a Ubuntu Kylin16.04(China version) and having trouble compiling my code, here are my gets function, the error received by the compiler]1
Implicit declaration of function means that you don't have function reference somewhere in header file normally.
Or maybe you have it, but the file where your function is called does not see this.
//Declaration, must always be before first function call
void myfunc(void);
//Function usage somewhere in the code
int main(void) {
//Call it here, compiler see the reference and knows what type
//of func it is and what parameters should be used to func
myfunc();
}
//Definition, write function content here
//This part will be compiled in separate way, linked will put everything together
void myfunc(void) {
//Write function content here
}
Related
I am trying to instrument the linux kernel code to insert a function call in every function right after a BitCast instruction.
So I modify the C code to #include <linux/my_header.h> where I have my printer function.
The header looks something like this.
#ifndef __header_ID
#define __header_ID
static inline void print_typecast(...){
printk(...);
}
#endif
Then I use Xclang to load my FunctionPass, which looks something like this.
// M is of type llvm::Module*
Function* f = M->getFunction("print_typecast");
if (f == nullptr) {
errs() << "Function not found in the module\n";
}
else {
// insert function in the code
}
However, my pass never finds the function in the module. When I remove the static it will find the function but then the linker in the final compilation step will complain of duplicate definition.
Anyone knows how to make LLVM "see" static imported/included functions?
Edit: I have also gone to the extreme where I have the same function directly written in every c file of the kernel code (the ones that #include <linux/kernel.h>)
static means that all calls to this function will be visible to this compiller now, and by implication that if the compiler sees no such calls, then it can skip compiling any output for the function, because you as programmer have promised that noone will want it.
I'm writing a simple code in C language, and this works.
Which compiles and excutes with no errors, gives the expected output.
#include <stdio.h>
int main(void) {
struct SiteTemplate {
int views;
};
int visit(struct SiteTemplate *site) {
site -> views++;
return 0;
}
struct SiteTemplate site;
site.views = 0;
visit(&site);
printf("%d\n", site.views);
return 0;
}
But in my VS Code, with C_Cpp linting is on, my IDE shows the following error and other problems with it.
declaration is incompatible with previous "visit" (declared at line 8)
Having a screenshot of it:
This error linting is really confusing me since my code works with gcc, it doesn't show any error when compiling.
And also, if I move my struct and function definition to the global level instead of inside main(), then the errors don't exist anymore... But what's the error declaration is incompatible? Or is there any problem with my code?
Click here to view the another screenshot to save whitespaces of this page.
By the way, the version of my VS Code is 1.52.0, with default C_Cpp linting.
Nested function definition is not standard C, it's supported by compiler extensions. According to C standard, any function definition needs to appear outside of any other function definition.
Consider the following code:
a.c
#include <stdio.h>
#include "b.h"
static int a = 41;
static void test(void){
a++;
printf("a: %d\n", a);
}
int main(void){
set_callback(test);
call();
return 0;
}
b.c
static void (*callback)(void);
void set_callback(void (*func)(void)){
callback = func;
}
void call(void){
if (callback){
callback();
}
}
b.h
void set_callback(void (*func)(void));
void call(void);
This compiles without warnings with -Wall and prints out a: 42 as expected.
Now, this might not be the best practice, since the writer of a.c probably doesn't expect test() to be called from another file and the variable a modified, but is this legal C code? Will it work portably on different platforms and compilers?
Yes, this is perfectly fine code and even good code. There is no need for your test callback to be global.
The compiler is responsible to ensure that the function isn't called outside the translation unit, before doing any optimizations that would affect calling it from outside.
If it sees that a pointer to the function is passed to an external function, it has to refrain from doing incompatible optimizations to the function.
Thus, the only effect is that the object file won't export a test symbol (This is termed Internal Linkage).
You wrote,
Now, this might not be the best practice, since the writer of a.c probably doesn't expect test() to be called from another file and the variable a modified, but is this legal C code?
If the writer did not want his test() function to be called from another file, he should not have passed a pointer to it to an outside module!
When the writer called set_callback(test); he knew he was passing his static method to an outside module, and giving that outside module permission to call it.
The point is that the author is in charge of the test method and where it goes. He's not prevented from doing anything, but he can control where his data goes; and he chose to pass it to an outsider.
I am following along a tutorial for C programming in 6502 Assembly and am having a great deal of difficulty on the 3rd step. When compiling my code in the command line, i get these errors:
test4.c(8): Error: '{' expected
test4.c(9): Warning: Function must be extern
test4.c(9): Error: ';' expected
test4.c(13): Error: '}' expected
I am using a program to compile .c files made in code::blocks to .nes files. The current tutorial is having me also make .s assembly file when compiling in the cl65 command line in Windows from the program that is compiling it. Here is the link to the tutorial page i am on:
https://helloacm.com/tutorial-3-c-programming-in-6502-using-assembly/
I have tried many different combinations of code that i can think of to try and get rid of a least some of the problems, but alas to no avail. I am an amateur in C, usually use C++ but i cannot and still trying to figure this out. I was not able to find the "Function must be extern" error anywhere with a quick google search either. Anyone have an idea what's going on?
Here is how i wrote the code in code::blocks:
void main()
{
asm("nop");
}
void testasm()
void main()
{
while(1) {
testasm(); // call the assembled function
}
}
Also, had a really difficult time following along on this particular tutorial part.
Thanks in advance, any help is appreciated on diagnosing the problem!
Perhaps this is what you're after?
void testasm()
{
asm("nop");
}
void main()
{
while(1) {
testasm(); // call the assembled function
}
}
Your code had two main() functions, and a prototype void testasm() with no terminating semicolon.
Alternatively, if testasm is written in assembly, your code should look like this (removing the extra main function):
extern void testasm(); // `extern` not specifically required, but may be for
// your particular compiler
void main()
{
while(1) {
testasm(); // call the assembled function
}
}
You need to be much more careful writing code.
This is concerning one of the functions in my header file(battleship.h).
Here is the code for my function prototype
int gameOver();.
Inside my main C file, here is my code for using that function and including that header file
#include "battleship.c"
int main ()
{
while(!gameover)
{
......
}
}
When I tried compiling the main c file(repo.c) into object code with this command
gcc -c repo.c
I got the warning "the address of 'gameOver', will always evaluate as 'true'. Is there any way I can address this warning?
You probably intended to call the function instead of simply taking its address:
while(!gameOver())
{
...
Note the parentheses.