Implicit declaration of function ‘str[n]casecmp’ [-Werror=implicit-function-declaration] - c

I am compiling a C library, using C99. I am including string.h to my translation unit (and I can navigate to the definitions of the str?casecmp functions in my NetBeans IDE.
The source looks something like this:
#include <string.h>
int foo(char* c1, char* c2) {
return strcasecmp(c1, c2);
}
int foobar(char* c1, char* c2, int n) {
return strncasecmp(c1, c2, n);
}
However, when I attempt to compile, I get the error shown in the title.
I am using GCC version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5).
This is what my gcc command looks like:
gcc -c -g -Werror -DE4C_THREADSAFE -DLINUX_BUILD -I../include -I../genutils -std=c99 -fPIC -MMD -MP -MF build/Debug/GNU-Linux-x86/btypes.o.d -o build/Debug/GNU-Linux-x86/btypes.o btypes.c
What is causing this, and how do I fix it?

These functions are declared in strings.h, not string.h.

Include the header strings.h instead of string.h.

Related

Error when using the powf() function in C

I wrote this C code to find the value for 3 squared.
#include <stdio.h>
#include <math.h>
int main( void )
{
float a;
a = powf( 3, 2 );
printf( "\n%f\n", a );
return 0;
}
I get the warning implicit declaration of function 'powf' even with math.h library included and -lm in the terminal command:
gcc -o test.exe -ansi -Wall test.c -lm
My gcc version is 4.2.2 if it helps.
powf is added in C99. Option -ansi is equivalent to -std=c89. You need to use -std=c99 flag.
gcc -o test.exe -std=c99 -Wall test.c -lm
The problem is the -ansi parameter. This is equivalent to -std=c90.
As the man page for powf states, you need to use -std=c99

Issue passing a function as a parameter in C

I'm new to C and stuck on this. The answer here seems widely accepted, but I believe I'm following the correct format and still getting issues.
Here's the offending line in main() (in main.c)
PrintWrapper(PrintFunction, *decoded); // *decoded is a char
The PrintFunction (also in main.c):
void PrintFunction(char c)
{
printf("%c", c);
}
And the prototype declaration (in p1a2.h - in the same directory as main.c)
extern void PrintWrapper(void (*)(char), char c);
The actual source is hidden away in printwrapper.o (same directory as main.c). This is for an assignment and is a very contrived usage of passing a function as a parameter. I compile the program with
g++ main.c printwrapper.o -Wall -g -o tnine
and get the compiler error (the backtick is not a typo if that is relevant)
<path>/main.c:42: undefined reference to `PrintWrapper(void (*)(char), char'
Why does this happen? I'm hoping it's me overlooking something simple.
As you are using g++, the compiler expects the function name to be mangled (for more information see this wikipedia article.
To fix the issue, use gcc instead of g++ or try the following:
extern "C" void PrintWrapper(void (*)(char), char*);
The code you showed us is valid.
Looks like printwrapper.o just don't contain such function.
You should use gcc instead of g++ to compile C code. The latter front-end is for C++ code, which symbols are subject to name mangling. Assuming that you are really asking about C language, the proper way to compile your project is:
gcc main.c printwrapper.o -Wall -g -o tnine
Here is some working sample:
main.c
#include <stdio.h>
#include "pla2.h"
void PrintFunction(char c)
{
printf("%c\n", c);
}
int main(void)
{
char c = 'a';
char *decoded = &c;
PrintWrapper(PrintFunction, *decoded);
return 0;
}
pla2.h
extern void PrintWrapper(void (*)(char), char c);
printwrapper.c
extern void PrintWrapper(void (*f)(char), char c)
{
f(c);
}
then:
$ gcc -c printwrapper.c
$ gcc main.c printwrapper.o -Wall -g -o tnine
$ ./tnine
a

Linking multiple .c files

I have a C file named first.c in which I define an array and call a function which is defined in a C file named second.c. This is how first.c looks:
int main(void)
{
int array[100];
myFunc(*array);
}
second.c on the other hand looks like this:
void myFunc(int array)
{
...
}
But anytime I try to compile these, second.c gives me errors as if it had no idea about the array I passed to its function as an argument. I guess the function doesn't know that at the linking stage. I compile these like this:
gcc -O2 -std=c99 -Wall -pedantic -lm second.c -c
gcc -O2 -std=c99 -Wall -pedantic first.c -c
gcc second.o first.o -o finished
But that's just what I came up with and of course it doesn't work. I guess a Makefile would be in place, but I'm not sure how to implement it.
Your issue may lie in that the received value is not a pointer- so change void myFunc(int array) to void myFunc(int* array).

What's wrong in creating/using a shared library with gcc here?

In libname.h:
int add_libname(int, int);
In libname.c:
#include "libname.h"
int add_libname(int a, int b)
{
return a+b;
}
I can build the shared library this way:
gcc -shared -fPIC libname.c -o libname.so
But I can't use it in another programe test.c:
#include <stdio.h>
#include "libname.h"
int main(int argc, char* argv[])
{
printf("%d\n", add_libname(1,5));
}
Reporting undefined reference to add_libname when I try to build it..
What's wrong here?
Because add_libname takes (int, int) you're giving it (1+5 = 6) or just (int)
I think you meant
add_libname(1, 5);
Also to compile it correctly you must use gcc like so
gcc -o myapp test.c -L. -lname
the lib part of libname is ignored as it is implicit
To create a shared library use these
gcc -fPIC -c libname.c
it gives warning: position independent code and libname.o file is generated.
and now type these command,
gcc -shared libname.so libname.o
libname.so ( the shared library is created with .so extension). To use the shared library
gcc -I/give the path of libname.h sourcefile.c /give the path of your .so file
example if your c file is file.c and the header file libname.h is in c:\folder1\project and your libname.so (shared library) is in c:\folder\project2
then
gcc -I/cygdrive/c/folder1/project file.c /cygdrive/c/folder/project/libname.so
this is the gcc command to be used while using the shared library.
Thank you.

Why does CC not see my function definition in header?

I'm writing a simple application in ANSI C. I am using GCC in a Unix environment.
I have the following sample application:
//main.c
#include "foo.h"
int main()
{
int result;
result = add(1,5);
return0;
}
Header:
//foo.h
#ifndef FOO_H_INCLUDED
#define FF_H_INCLUDED
int add(int a, int b);
#endif
Implementation:
//foo.c
int add(int a, int b)
{
return a+b;
}
I am compiling my program with the following command:
cc main.c -o main.o
The compiler complains that 'reference to add is undefined'. Is this a linking problem? How do properly make use of my header?
Thanks!
You need to compile both your source files together:
cc main.c foo.c -o main
Also, in this case, -o produces an executable, so calling it main.o can be misleading.
Yet another tidbit, though unrelated to the question: the #ifndef and #define in foo.h don't match.
The header is not your current problem. Your current problem is that you're not compiling the add function definition in foo.c.
Try
cc main.c foo.c -o main.o
If you are trying to compile main.c into an assembled object file, you need to prevent gcc from trying to link. This is done via
cc -c main.c -o main.o
You can compile all other object files, then when you have all of your object files ready, you simply do
cc main.o obj1.o anotherOBJ.o -o myExecutableBinary
"undefined reference" is a linker error, not a compiler error.
The compiler sees the declaration in the header, but you have not compiled or linked the definition in foo.c. Your title uses the term definition incorrectly.

Resources