How do I use strcasestr()? - c

I #include <string.h> but when I call strcasestr(src, search); I get the following error message implicit declaration of function ‘strcasestr’. how to I compile: gcc-4.6 -Wall -lsqlite3 -lunac -Werror -O2 -o foo.out foo.c how to fix this? Thanks in advance.

As specified in the corresponding manpage, since strcasestr is a nonstandard extension you must #define _GNU_SOURCE before the #include <string.h> before any #include (other files may already include <string.h>, thanks #Cubbi for pointing out this potential problem); this can also easily be accomplished by specifying -D_GNU_SOURCE on the compiler command line.

You must add:
#define _GNU_SOURCE
before the string.h include, since the function is non-standard.

Related

unknown type name "pthread_barrier_t"

I am trying to parallelize an algorithm in C. I want to use pthread_barrier_t but my Ubuntu wsl can't find it for some reason. I have pthread.h included and I can use the rest of the pthread functions. libthread.a is installed.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
/* Error occurs here */
pthread_barrier_t barrier;
Exact error is: "identifier pthread_barrier_t is undefined"
I saw elsewhere it could be the way I'm compiling.
Compiling as follows:
gcc -o test test.c -Wall -std=c99 -lpthread -lm
Also, VS Code can't identify the function.
The problem is your -std=c99 option. Using strict C mode disables a bunch of stuff, including something that stops pthread_barrier_t from getting defined. If you use -std=gnu99 instead, it should compile. (Tested on Ubuntu 16.04 on WSL).
Alternatively, add
#define _XOPEN_SOURCE 600 /* Or higher */
or
#define _POSIX_C_SOURCE 200112L /* Or higher */
before the first #include in your source. See man 7 feature_test_macros for the acceptable values of these macros and more information.

F_SEAL_SEAL undeclared, even when headers are included

I'm trying to use file sealing on Linux. Here's an example C program.
#define _GNU_SOURCE
#include <unistd.h>
#include <fcntl.h>
int main(void) {
(void)F_SEAL_SEAL;
}
You can build it using gcc -Wall -o ./linux_file_sealing linux_file_sealing.c or similar.
When I build it, I get an error about F_SEAL_SEAL.
gcc -Wall -o ./linux_file_sealing linux_file_sealing.c
linux_file_sealing.c: In function ‘main’:
linux_file_sealing.c:7:19: error: ‘F_SEAL_SEAL’ undeclared (first use in this function)
printf("%d\n",F_SEAL_SEAL);
^
linux_file_sealing.c:7:19: note: each undeclared identifier is reported only once for each function it appears in
I'm including unistd.h and fcntl.h, as per the man page... so what else should I be doing, and where is that described?
(The man pages just say that sealing is "Linux-specific", but give no further details. This is the reason for including the GNU_SOURCE define, which is how you get the other Linux-specific stuff, but for F_SEAL_SEAL it seems to make no difference.)
(Ubuntu 16.04 LTS, Linux 4.4.0-36)
You want
#include <linux/fcntl.h>
instead of
#include <fcntl.h>

Is there a way to ask gcc to treat #include <> like #include ""?

Is there a compiler or preprocessor flag that will force gcc to treat #include <x.h> like it would #include "x.h"? I have a bunch of generated code that uses #include <> for files in the current directory, and gcc reports No such file or directory for these files. I'm looking for a workaround that doesn't involve editing the code.
EDIT: -I. doesn't do it. Let's say I have the following files:
foo/foo.h:
#include <foo2.h>
foo/foo2.h:
#define FOO 12345
xyz/xyz.c
#include <stdio.h>
#include "foo/foo2.h"
int main(void)
{
printf("FOO is %d\n", FOO);
return 0;
}
If, inside the xyz directory, I compile with gcc -o xyz I.. xyz.c, the compile fails:
In file included from xyz.c:2:
../foo/foo.h:1:18: error: foo2.h: No such file or directory
xyz.c: In function ‘main’:
xyz.c:6: error: ‘FOO’ undeclared (first use in this function)
xyz.c:6: error: (Each undeclared identifier is reported only once
xyz.c:6: error: for each function it appears in.)
Adding -I. doesn't change anything.
But, if I change foo/foo.h to:
#include "foo2.h"
Then the compile works. I know I could add -I../foo to my command line, but I was looking for a more generic way to treat #include <> as #include "". Does one exist?
Yes, you can pass the switch -I . to the compiler to add the current directory to the include search path.
The -I- option might help you. From gcc's man page:
-I- Split the include path. Any directories specified with -I options
before -I- are searched only for headers requested with
"#include "file""; they are not searched for "#include <file>". If
additional directories are specified with -I options after the -I-,
those directories are searched for all #include directives.

warning: implicit declaration of function 'kill'

I am making these inclusions:
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
But still getting this warning.
You are probably passing the "-ansi -Wall" switches to the gcc compiler.
You can remove "-ansi" if you don't need it, otherwise try to set the proper feature define macro.
Something like:
#define _POSIX_SOURCE
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
You can also define it on the compile time by adding this flag with gcc.
-D_POSIX_C_SOURCE
ex:
-g -D_POSIX_C_SOURCE -Wall -std=c99
According to the kill(2) Linux manual page, _POSIX_C_SOURCE must be #defined before any includes in order for the prototype of the function kill() to be included through <signal.h> if you are using glibc.
Your code will probably compile either way, since the binary is available for linking regardless of the defines, but that's the proper way to silence this warning according to the documentation.

unistd.h and c99 on Linux

This simple .c file:
#include <unistd.h>
void test() {
char string[40];
gethostname(string,40);
}
... when compiled normally, works fine:
$ cc -Wall -c -o tmp.o tmp.c
$
... but when compiled in C99 mode, gives a warning:
$ cc -Wall -std=c99 -c -o tmp.o tmp.c
tmp.c: In function `test':
tmp.c:5: warning: implicit declaration of function `gethostname'
$
The resultant .o file is fine, and linking works. I'd just like to get rid of the warning. I can achieve this in a hacky way, by putting declarations in my own .h file.
What is it about C99 that means the declarations in unistd.h don't get included?
Can this be overcome, without giving up the niceness of C99?
I see the same problem for other standard libs.
You may need to define some macros in a particluar way to get the prototype for gethostname()
From man gethostname:
Feature Test Macro Requirements for
glibc (see feature_test_macros(7)):
gethostname(): _BSD_SOURCE || _XOPEN_SOURCE >= 500
sethostname(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
So:
#define _BSD_SOURCE
#include <unistd.h>
void test() {
char string[40];
gethostname(string,40);
}
The gory details:
If you don't specify the -std-c99 option, then features.h (which is implicitly included by unistd.h) will default to setting _BSD_SOURCE in such a way that the prototype for gethostname() gets included. However, specifying -std=c99 causes the compiler to automatically define __STRICT_ANSI__, which in turn causes features.h to not define _BSD_SOURCE, unless you force it with your own feature macro definition (as above).
gethostname( ) is not a standard C function (it's not mentioned anywhere in the C99 standard), so the symbol is correctly not defined when compiling to the standard.
If you're using the gcc toolchain, use -std=gnu99 and you'll get the behavior you want.
Alternatively, looking at <features.h>, it seems like you could use -D_GNU_SOURCE or -D_XOPEN_SOURCE=500 to get the desired behavior.
Read man gethostname. It says in the Feature Test Macro Requirements, that _BSD_SOURCE (or _XOPEN_SOURCE>500) is required to pull gethostname from unistd.h.
Next read man feature_test_macros. You will find that -std=c99 turns on __STRICT_ANSI__ which in turns off _BSD_SOURCE. This means you can't get gethostname from unistd.h unless you define _BSD_SOURCE again. I usually place _GNU_SOURCE on my command line (i.e. gcc -D_GNU_SOURCE -std=c99 file.c) for most things, which turns on _BSD_SOURCE as well.
P.S. The manual page contains an example program which can print the current ft-macros. You might compile and run it for some compiler settings.

Resources