C .pc file warning - c

I have a RefTables.pc file.
When I execute the make command, I get this warning:
RefTables.c:109: warning: type defaults to `int' in declaration of `sqlcxt'
RefTables.c:111: warning: type defaults to `int' in declaration of `sqlcx2t'
RefTables.c:113: warning: type defaults to `int' in declaration of `sqlbuft'
RefTables.c:114: warning: type defaults to `int' in declaration of `sqlgs2t'
RefTables.c:115: warning: type defaults to `int' in declaration of `sqlorat'
How can I remove it?
I am using linux & gcc compiler.

It's been a while since I used Pro*C, but I think you can add a command line option to the proc command line
code=ANSI_C
which will give prototypes for the functions named.

You can remove the warning by specifying the type of the 5 offending declarations. Actually, they must be declared with no type at all, which defaults to int in C (but generates a warning).
Edit: I found on Google this declaration.
extern sqlcxt (/*_ void **, unsigned int *, struct sqlexd *, struct sqlcxp * _*/);
The function has no return type. It should have one. Write it as follows.
extern int sqlcxt (/*_ void **, unsigned int *, struct sqlexd *, struct sqlcxp * _*/);
Or you can manually state in the compiler command line to ignore these warnings. They won't be displayed anymore.

In the future, provide a code snippet along with the warnings so that we have some context to work from. Otherwise we can only guess at what the real problem is.
I'm assuming that sqlcxt, sqlcx2t, etc., are functions. Without seeing the source code, it sounds like you don't have a declaration for those functions in scope before using them.
Here's a short example of what I mean:
int main(void)
{
foo();
return 0;
}
void foo(void)
{
// do something interesting
}
When the compiler sees the call to foo in main, it doesn't have a declaration in scope, so it assumes that foo returns int, not void, and will return a warning similar to what you got above.
If your functions are defined in the same file as they are called, there are two ways around this problem. My preferred way is to define the function before it is used:
void foo(void)
{
// do something interesting
}
int main(void)
{
foo();
return 0;
}
Another way is to have a declaration of the function in scope before calling it:
void foo(void);
int main(void)
{
foo();
return 0;
}
void foo(void)
{
// do something interesting
}
It sounds like these functions are part of a database API; if so, there should be a header file that contains declarations for those functions, and that header should be included in your source file:
/** foo.c */
#include "foo.h"
void foo(void)
{
// do something interesting
}
/** end foo.c */
/** foo.h */
#ifndef FOO_H
#define FOO_H
void foo(void);
#endif
/** end foo.h */
/** main.c */
#include "foo.h"
int main(void)
{
foo();
return 0;
}
/** end main.c */
Hope that helps.

Related

Makefiles and structures in C

EDIT: To try and make things easier for the kind souls trying to help me, here are a couple links that should make things more clear:
Pre-makefile repl
Post-makefile repl
A little background on the assignment: We are supposed to take a program we wrote last week and break up the separate functions into their own files and use a makefile to compile and link and all that stuff. This is my original program (basically, it reads a file of name number and stores them in a struct, then uses the command line arguments to search for a name).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _data {
char* name;
long number;
};
int SCAN(FILE *(*input)) {
int lines = 0;
char word[50];
long num = 0;
while (1) {
fscanf(*input,"%s %ld",word, &num);
lines++;
if (feof(*input)) break;
}
return lines;
}
struct _data *LOAD(FILE *input, int size) {
char* line = NULL;
size_t len = 0;
int i=0;
rewind(input);
struct _data *book = calloc(size,sizeof(struct _data));
for (i = 0;i<size;i++) {
getline(&line, &len, input);
book[i].name = calloc(len+1,sizeof(char));
strcpy(book[i].name,strtok(line," "));
book[i].number = atoi(strtok(NULL, " "));
}
return book;
}
void SEARCH(struct _data *BlackBook, char *name, int size) {
int i;
for (i=0;i<size;i++) {
if (strcmp(name,BlackBook[i].name) == 0) {
printf("*******************************************\n");
printf("The name was found at the %d entry.\n",i+1);
printf("*******************************************\n");
break;
}
//If we reach the end of the array and name was not found
if (i == size-1) {
printf("*******************************************\n");
printf("The name was NOT found.\n");
printf("*******************************************\n");
}
}
}
void FREE(struct _data *BlackBook, int size) {
int i;
for (i=0;i<size;i++){
free(BlackBook[i].name);
}
free(BlackBook);
}
//MAIN DRIVER ===================
int main(int argv, char** argc) {
int size;
char* filename = "hw5.data";
FILE *input = fopen(filename,"r");
size = SCAN(&input);
struct _data *phone_book = LOAD(input,size);
fclose(input);
//Check a name is given. If so, search
if (argv < 2) {
printf("*******************************************\n");
printf("* You must include a name to search for. *\n");
printf("*******************************************\n");
} else {
SEARCH(phone_book, argc[1], size);
}
FREE(phone_book,size);
return 0;
}
When I make my makefile, I can get the SCAN and LOAD functions to work appropriately. But, when I try to put either SEARCH or FREE into their own files, my compiler freaks out and I get warnings like:
In file included from hw6-free.c:1:0:
hw6-free.h:9:18: warning: ‘struct _data’ declared inside parameter list
void FREE(struct _data *BlackBook, int size);
^
hw6-free.h:9:18: warning: its scope is only this definition or declaration, which is probably not what you want
hw6-free.c:3:18: warning: ‘struct _data’ declared inside parameter list
void FREE(struct _data *BlackBook, int size) {
^
hw6-free.c:3:6: error: conflicting types for ‘FREE’
void FREE(struct _data *BlackBook, int size) {
^
In file included from hw6-free.c:1:0:
hw6-free.h:9:6: note: previous declaration of ‘FREE’ was here
void FREE(struct _data *BlackBook, int size);
^
hw6-free.c: In function ‘FREE’:
hw6-free.c:6:5: error: invalid use of undefined type ‘struct _data’
free(BlackBook[i].name);
^
hw6-free.c:6:19: error: dereferencing pointer to incomplete type ‘struct _data’
free(BlackBook[i].name);
^
Makefile:20: recipe for target 'hw6-free.o' failed
make: *** [hw6-free.o] Error 1
And reading through it, it looks like the fact my program takes a struct as an argument is my main problem? My 'post-makefile' program looks like:
#include "hw6-main.h"
int main(int argv, char** argc) {
int size;
char* filename = "hw5.data";
FILE *input = fopen(filename,"r");
size = SCAN(&input);
struct _data *phone_book = LOAD(input,size);
fclose(input);
//Check a name is given. If so, search
if (argv < 2) {
printf("*******************************************\n");
printf("* You must include a name to search for. *\n");
printf("*******************************************\n");
} else {
SEARCH(phone_book, argc[1], size);
}
FREE(phone_book,size);
return 0;
}
And my makefile looks like:
DEP = hw6-scan.o hw6-load.o hw6-search.o hw6-free.o hw6-main.o
HDR = hw6-scan.h hw6-load.h hw6-search.h hw6-free.h hw6-main.h
NAME = output
all: $(NAME)
output: $(DEP) $(HDR)
gcc $(DEP) $(HDR) -o $(NAME)
hw6-scan.o: hw6-scan.c
gcc -c hw6-scan.c
hw6-load.o: hw6-load.c
gcc -c hw6-load.c
hw6-search.o: hw6-search.c
gcc -c hw6-search.c
hw6-free.o: hw6-free.c
gcc -c hw6-free.c
hw6-main.o: hw6-main.c
gcc -c hw6-main.c
clean:
rm *.o *.gch *.out output testfile
As an example, my hw6-free.c and hw6-free.h look like:
#include "hw6-free.h"
void FREE(struct _data *BlackBook, int size) {
int i;
for (i=0;i<size;i++){
free(BlackBook[i].name);
}
free(BlackBook);
}
and
#include <stdio.h>
#include <stdlib.h>
void FREE(struct _data *BlackBook, int size);
respectively.
And, finally, I defined the struct in the hw6-load.h file, along with a function prototype. Is that also a problem? Should I be defining it elsewhere?
I am SO SORRY for the long post, but I have been working on the for 10 hours and I'm about to throw my computer of a cliff.
THANK YOU FOR YOUR HELP STACK OVERFLOW!
This is an in-depth explanation of the details. It might be a bit too much, but if you keep reading, you may get a much deeper understanding of the language than if you just learn to write code that your compiler is ok with.
You are using your struct before you have declared it. Keep in mind that an #include is just a fancy way of telling your compiler: "Please, paste the contents of this .h file at this spot." The result of this pasting must read like valid code to the compiler.
It is true that the compiler's error message looks a bit weird. It certainly does not say "You used struct _data before you declared it". The reason for this is, that structs are implicitly declared at their first use. So, if you declare a variable with struct foo* bar; and the compiler has never seen a struct foo before, it will immediately consider struct foo as being declared, and a pointer variable to this struct foo of unknown size and shape will be defined. Likewise, when your compiler sees the function declaration
void foo(struct bar *baz);
it sees that it doesn't know a struct bar and implicitly declares one. Because this struct declaration happens inside a function declaration, the resulting type is declared local to the declaration at hand. As such, it is impossible for calling code to pass an argument of the correct type into this function, or even to implement the function in a separate statement. Such a function declaration is always useless. This is what the warning text "its scope is only this definition or declaration, which is probably not what you want" means: The compiler writers knew that such a declaration is bullshit, but it's legal C as far as the standard is concerned, so they compile it, but warn about it.
Ok, let's get to the actual error message. As the compiler tells you, the struct _data was only declared for the function declaration at hand. When your compiler later sees the function implementation, it stumbles across the undeclared struct _data a second time. Again, it implicitly declares a local struct type which is distinct from the previously implicitly declared type. Because those two implicitly declared types are distinct, so are the signatures of the declared functions. However, C mandates that a function can only have one signature, so the compiler produces the error "conflicting types for ‘FREE’".
You can try this out with this simple code:
void foo(struct bar* baz); //warning: local declaration of `struct bar`
void foo(struct bar* baz); //repeated warning + conflicting types error
So, how to fix this?
Simple. Declare your struct before you use it. That way you avoid its implicit declaration. The following code compiles fine:
struct bar; //global declaration of `struct bar`
void foo(struct bar* baz); //`struct bar` is known and the global declaration is used
void foo(struct bar* baz); //same as above, because this uses the same global declaration of `struct bar`, this redeclaration of `foo()` is ok
The declaration of the struct _data belongs into the header file that declares the functions which use struct _data as arguments.
Idiomatic declarations
Usually, types are declared with a typedef. This allows the code to omit the struct keyword when declaring variables. This takes one of two idiomatic forms:
To have a type with public members (pure data, no object in the OO sense), put the struct definition into the header:
typedef struct foo //`struct foo` is declared implicitly here
{ //it is also defined (= inner details are given) right here
int bar; //its member variables are defined
} baz; //this concludes the typedef, giving `struct foo` a second name
//`struct foo` and `baz` are now equivalent.
Usually, the two names will be the same or very similar, so the cleaned definition looks like this:
typedef struct foo {
int bar;
} foo;
//declarations of the functions that use `struct foo`
...
If the type is an object that should keep its data members to itself, the declaration and definitions are split like this:
Inside foo.h:
typedef struct foo foo; //declare that `struct foo` == `foo` exists, but don't give details
//declare the functions working on a `foo`
void foo_bim(foo* me);
void foo_bam(foo* me, ...);
...
Inside foo.c:
#include "foo.h" //so that the compiler may check that the function declarations in the header agree with the implementations in this file
struct foo { //define the size and shape of `struct foo` == `foo`
int bar;
};
//now only this file knows how a `struct foo` actually looks like
//implement the member functions of `foo`
void foo_bim(foo* me) {
...
}
void foo_bam(foo* me, ...) {
...
}
Note that the typedef ... non-struct-name; is purely optional in both cases, and there are quite a few programmers who want to see the struct keyword wherever a struct is used (like a certain Mr. Torvalds). These programmers simply leave off the typedef ... non-struct-name; part, otherwise they use the idioms above in the same way. I have described the full-featured version here, to ensure that you'll not be surprised when you first see the typedef construct.
I'm not sure what you did. But to step back: keep clear in your mind the difference between declarations and definitions. A declaration shows a variable or function signature, but does not create any variables of that type or implement that function. A definition declares a new variable (of some type) or implements a function. A declaration of a type just states that it exists (basically). A definition of a type shows its structure and members.
So, a definition of your type would be:
struct _data {
char* name;
long number;
};
And a declaration of a function would be:
void FREE(struct _data *BlackBook, int size);
and a definition of a function would be:
void FREE(struct _data *BlackBook, int size) {
int i;
for (i=0;i<size;i++){
free(BlackBook[i].name);
}
free(BlackBook);
}
So here're the rules:
Only #include header files in other files, never #include source files.
If a type is needed in more than one file, put its definition into a header file and #include that header file in all source files that use the type.
If a function is used in more than one file, put the declaration of that function into a header file and #include that header file in all the source files that use the function, including the source file containing the definition of the function.
If you follow these rules you'll never run into duplicate definitions at link time: you can't have duplicate definitions if you link each source file only one time and no included file contains a definition.
Type declarations can be useful, and breaking some of these rules can be useful, but for the work you're doing that's probably not worth worrying about.
Try to put header of struct declaration at top of files which uses it and be sure you’ve imported the file which contains struct declaration.

Getting the error 'conflicting type for function',why?

Here is the below:why it comes out?
#include <stdio.h>
void iniStudentLink(struct STUDENT_LINK * L);
int main(){
return 0;
}
void iniStudentLink(struct STUDENT_LINK * L){
printf("hello world!\n");
}
showing the error : conflicting types for inniStudentLink.
These are the issues that come out of gcc when compiling your code (something it would have been handy to include in your question to make it more complete, that's just advice for the future):
testprog.c:3:28: warning: 'struct STUDENT_LINK' declared inside
parameter list will not be visible outside of
this definition or declaration
void iniStudentLink(struct STUDENT_LINK * L);
^~~~~~~~~~~~
testprog.c:9:28: warning: 'struct STUDENT_LINK' declared inside
parameter list will not be visible outside of
this definition or declaration
void iniStudentLink(struct STUDENT_LINK * L){
^~~~~~~~~~~~
testprog.c:9:6: error: conflicting types for ‘iniStudentLink’
void iniStudentLink(struct STUDENT_LINK * L){
^~~~~~~~~~~~~~
testprog.c:3:6: note: previous declaration of ‘iniStudentLink’ was here
void iniStudentLink(struct STUDENT_LINK * L);
^~~~~~~~~~~~~~
In other words, you are declaring two independent instances of the structure, without actually defining it(a). The reason they are considered independent is because their scope is limited to the actual function where they are being declared.
You can fix this by actually defining it so that the declarations both refer to that definition, such as with (before any other use):
struct STUDENT_LINK { int some_data; };
In other words, this compiles just fine:
#include <stdio.h>
struct STUDENT_LINK { int some_data; };
void iniStudentLink (struct STUDENT_LINK *L);
int main(void) { return 0; }
void iniStudentLink (struct STUDENT_LINK *L){ puts("hi!"); }
(although it may should warn you about the fact you don't actually use L in the function).
(a) The basic difference between declaring and defining in C is this:
Declaring means declaring that something exists without creating it, such as (in your case) stating that you want to pass a pointer-to-it to a function.
Defining it means literally that, defining what it is rather than just that it is.
Example declarations are extern int i; or struct xyzzy; wheras equivalent definitions would be int i; and struct xyzzy { int plugh; };.

Declaration is incompatible

I was working with IAR Embedded Workbench, using C language.
I had some trouble while dividing my project into the usual main/.h/.c form.
For example, if i create an example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
void function(int [], int);
#endif
And than an example.c
#include "example.h"
void function (int[] array, int number)
{number = 1; //code
}
It says:
Error[Pe147]: declaration is incompatible with "__interwork __softfp
void function(int *, int)" (declared at line 4 of (path)
Error[Pe141]: unnamed prototyped parameters not allowed when body is present (path)
Error[Pe020]: identifier "number" is undefined (path)
Error while running C/C++ Compiler
The problem is in void function(int [], int). Change to void function(int name[], int) or void function(int *, int). Another error is in int[] array - it has to be int array[] or int * array.
You use wrong syntax. Look at
void function (int array[], int number)
{ number = 1; //code
}
In IAR, you will see this error when declaration and definition will not be matching.
For Ex- if you declare your variable as __ro_placement in .hpp and during initialization in .c or .cpp, if you will not provide __ro_placement with variable, IAR will throw same error.

Assigning a void pointer from function works in one source file, but not in two

So I'm working through this chapter of a sweet modern OpenGL tutorial, and in part of the code I assign a void pointer variable to the return value of a void * function from a second file. I got this error from gcc: initialization makes pointer from integer without a cast. As a test, I tried merging the two files and compiling again. This time, with the function in the same file as where it's called, I get no error.
I was able to reproduce this with three minimal files:
file1.c
void main() {
void *newval = foo();
}
file2.c
#include <stdlib.h>
void *foo() {
void *val;
val = malloc(10);
return val;
}
file3.c
#include <stdlib.h>
void *foo() {
void *val;
val = malloc(10);
return val;
}
void main() {
void *newval = foo();
}
file3.c is just the first two combined. My question is why when I compile one way I get an error, and the other way I get none:
$gcc file1.c file2.cfile1.c: In function ‘main’: file1.c:2:17:
warning: initialization makes pointer from integer without a cast
[enabled by default]$gcc file3.c$
I thought that the file1.c + file2.c combo was identical to file3.c. What am I missing?
In your file1.c, it seems the declaration of void *foo() is missing.
You can either include the header file containing this declaration or add line void *foo() on top of file1.c.
When compiler finds symbol foo followed by ( in main file, and there was no declaration, it assumes foo accepts any number of arguments and returns an int. So you see the warning that int is being converted to a pointer.
But in file file3.c, symbol foo is known as it is a defined function before its first usage so compiler knows that foo returns a pointer and thus no warning.
You need to declare the foo() function so that compiler knows what it is when it compiles file1.c.
Create file2.h and add in it
void *foo();
add include it in file.c
#include "file2.h"
Without declaration, the compiler assumes all unknown functions as returning int, but as your function returns void * and you are assigning it to void *, its trying to assign int to void * and hence you are getting that warning.

Is There C Syntax For Function Pointer From Function Declaration

Instead of declaring a function pointer typedef for a function, is it possible to get it from the function declaration?
Typically,
int foo(int x);
typedef int (*fooFunc)(int);
fooFunc aFunc;
What I want:
int foo(int x);
foo* aFunc;
I want to use it for dlsym:
foo* aFunc;
aFunc = dlsym(lib, "foo");
aFunc(x);
If I update foo and forgot to update fooFunc, or vice versa, that would be bad. Also, I may have many functions and it would be more work to maintain both the function declarations and the function pointer typedefs that are associated with those functions.
Conclusion:
AndreyT's answer is the most portable but if you code for gcc then typeof is a great solution.
If you are talking about a declaration specifically, i.e. a non-defining declaration of a function, you can remove the redundancy by defining a typedef-name for function type and using it in both cases - to declare the function itself and to declare a pointer to it, like this
typedef int FuncType(int); /* <- function type */
FuncType foo; /* <- declaration of `int foo(int)` */
FuncType *aFunc; /* <- definition of `int (*aFunc)(int)` */
I.e. typedef-names can be used in non-defining function declarations. However, you can't use a typedef name in function definition, meaning that later you'll still have to do
int foo(int x) /* <- no way to use the above `FuncType` here */
{
/* whatever */
}
which basically renders the above trick virtually useless.
Of course, this doesn't help you to generate a pointer from an existing non-modifiable function declaration, if that's your situation.
If you have gcc, typeof works.
Update
$ cat fxf.c
#include <stdio.h>
int main(int argc, char **argv) {
typedef __typeof__ (main) function_of_same_type_as_main_t;
function_of_same_type_as_main_t *f;
printf("main() called.\n");
f = main;
if (argc) f(0, NULL);
return 0;
}
$ /usr/bin/gcc -std=c89 -pedantic -Wall -Wextra -o fxf fxf.c
fxf.c:3: warning: unused parameter ‘argv’
$ ./fxf
main() called.
main() called.
Simple answer: no, that doesn’t work. foo is a specific function which has a prototype (int (int)). Using foo in the way you did would be a bit like using an int to declare another int:
4 x; // expect this to be the same as int x
That said, there might be compiler extensions which make that work. I know that the upcoming C++ standard will have the decltype keyword to allow that. Using that, the following might work (untested, since I don’t have a supporting compiler handy):
int foo(int x);
decltype(&foo) aFunc = dlsym(lib, "foo");
It is not possible. However, you can write some code that would generate a warning, so that you would catch type mismatch. The following code generates an assignment from incompatible pointer type warning.
#include <stdio.h>
int foo(int, int);
typedef int(*fooFunc)(int);
fooFunc myfunc;
int foo(int x, int y)
{
return 2*x + y;
}
int main(int argc, char **argv)
{
myfunc = foo;
printf("myfunc : 0x%x\n", (unsigned int)myfunc);
return 0;
}
Of course, this means you would have to write this test code where the foo function is visible, so this is still more code to add for each function type. The solution here is probably a code generator, that would generate a proper header file containing both functions and their associated typedefs
Not quite the same, but you can typedef the function and use it for both the prototype and the pointer.
typedef int fooFunc(int);
fooFunc foo;
fooFunc *aFunc;

Resources