How can I compile code that uses getsubopt()? - c

I want to parse a list of options of the form key1=val1, key2=val2, etc (like the options to mount -o). The getsubopt() function seems perfect for this task (http://www.gnu.org/s/hello/manual/libc/Suboptions.html). However, when I try to compile my code using gcc, I get:
warning: implicit declaration of function ‘getsubopt’
and the program segfaults when I run it.
I added #include <stdlib.h> but the compiler doesn't pick up the declaration.

Do you have:
#define _XOPEN_SOURCE 500
#include <stdlib.h>
at the top of the file that contains the call to getsubopt? The error you are getting is what you would expect if you call a function which has not been declared.

Related

Assimp multiple definition error in material.h when linking across multiple compilation units

I'm following the example provided at https://assimp.sourceforge.net/lib_html/usage.html to import a simple model using the assimp library. The first three lines in the example are the required include statements:
#include <assimp/cimport.h> // Plain-C interface
#include <assimp/scene.h> // Output data structure
#include <assimp/postprocess.h> // Post processing flags
Of these, scene.h seems to be problematic. If you include it AND link against a compilation unit that also includes it, it gives multiple definition errors for aiGetMaterialFloat and aiGetMaterialInteger. A stripped down example is given below:
main.c:
#include <assimp/cimport.h> // Plain-C interface
#include <assimp/scene.h> // Output data structure
#include <assimp/postprocess.h> // Post processing flags
#include "model.h"
//...
model.h:
#include <assimp/cimport.h> // Plain-C interface
#include <assimp/scene.h> // Output data structure
#include <assimp/postprocess.h> // Post processing flags
//...
model.c
#include "model.h"
makefile:
main: main.c model
gcc main.c model.o
model: model.c model.h
gcc -c model.c
I'm using MinGW to compile these.
Now this DOES seem to compile if:
main.c does not include model.h, OR
the include statements are removed from model.h, OR
model.o is not linked when compiling main.c, OR
the import for scene.h is removed from one or both files
It will also compile just fine if the #includes are all moved to main.c and the model compilation unit is removed entirely.
Note that if one of the above conditions are met, this compiles just fine without a DLL or static library linked. The problem seems to be exclusively from including the header files.
I've done a lot of research and the only thing I could find is that it might be the inline specifier on these functions that was causing the issue. However, after removing the inline specifiers from the function definitions in the library header file, I'm still getting the same errors. I've been trying to solve this issue for hours and nothing seems to be working. Any ideas?

error trying to #include headerfile within main function

Newbie question: To my knowledge the way #include works is that it copy-pastes whatever is inside the included file to the designated spot. All I want is to #include a file which only holds a printf().
And while this works perfectly in any online IDE I have tried, in Code::Blocks I receive
error: expected declaration specifiers or '...' before string constant
This is the main file:
#include <stdio.h>
int main(void)
{
#include "null.h"
}
And this is the included null.h:
printf("Hello, world\n");
I have tried #include <stdio.h> above printf() function call as well as calling the file null.c however nothing seems to work. I have searched Google but can't wrap my head around what this error has to do with any of the explanations provided.
edit: I now use gcc via cmd in order to compile which works for me.

string.h and stdlib.h functions give me the error: undefined reference to '...'

I'm modifing a c project and is the first time I'm tryng to use the stdlib.h and string.h libraries in my project. I'm working on MCUXpresso (IDE based on eclipse). This is my code:
#include <string.h>
#include <stdlib.h>
#include "config.h"
int number=100;
int n1,n2;
char test[5]="test";
char str[5];
extern void fntest(TTASKTABLE *ptrTaskTable)
{
itoa (number,str,10);
n1=strlen(test);
n2=atoi(test);
}
As you can see I've included the header files but the compiler give the errors: undefined reference to 'itoa' ; undefined reference to 'strlen' ; undefined reference to 'atoi'
And in my includes folder there is already (by default on my project) the folder containing the standard libraries.
I see the functions are used in some other files in the project... I can't understand why I've this error.
In the original code the functions are in a body function, I've corrected that.
Can you help me please?
finally I've solved my problem !
Ijust had to change the linker settings: project-->properties-->C/C++ Build-->settings-->MCU Linker-->general and change form No startup or default libs to Do not use standard start files and then no more errors!
I don't know if you have posted your actual code, but at this stage as you have uploaded its not going to compile.
#include <string.h>
#include <stdlib.h>
#include "config.h"
int number=100; // This is okay
int n1, n2; // This is okay
char test[5]="test"; // This is okay
char str[5]; // This is okay
itoa (number,str,10); // This is wrong
n1=strlen(test); // This is wrong
n2=atoi(test); // This is wrong
Whatever that I have appended with a comment // This is wrong is because, these needs to be in a function body.
Now since you already stated that,
I see the functions are used in some other files in the project..
I don't have to provide you with a way to implement itoa function.
Update:
Since now that you have added it into a function, the point that I talked about is fixed. Now, it should ideally compile, provided that there a valid definition of a itoa function in one of the header files that have been included.

Linking md5.h library for HTTP Digest sample implementation

I am struggling to compile a simple C program from RFC 2617. The program is digtest.c and it uses digcalc.c, another file from the sample implementation. The latter one depends on two files that my compiler doesn't know about:
#include <global.h>
#include <md5.h>
At first I got this error:
digcalc.c:5:20: fatal error: global.h: No such file or directory
I resolved that by changing <global.h> to <stddef.h>, it seems. But I still get this error:
digcalc.c:7:17: fatal error: md5.h: No such file or directory
Now, md5.h seems to refer to the file found in libbsd. So I installed libbsd-dev and tried to compile the files like this:
gcc digcalc.c digtest.c -o digtest -L/usr/lib/x86_64-linux-gnu -lbsd
where /usr/lib/x86_64-linux-gnu is the location of libbsd.so and libbsd.a files. However, this does not resolve the last compilation error.
Could anyone point out what am I missing here?
Figured it out. Had to change <md5.h> to <bsd/md5.h>, as noted on libbsd page.
So instead of the original headers in digcalc.c:
#include <global.h>
#include <md5.h>
I used:
#include <stddef.h>
#include <bsd/md5.h>
Also had to change function stricmp to strcasecmp, its POSIX equivalent. After that the sample code compiled seamlessly.

how to use crypt( ) method in Linux?

I just want to use crypt() to generate an encrypted password,and I write a demo which invoke the crypt() method.
Here is my code
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("%s\n",crypt("abc","ab"));
exit(0);
}
I compile it using "gcc tem.c -lcrypt' and when I run it, everything seems right, but a "segment error" shows up. so please tell me what's wrong with this simple program?
If you compile with the flag -Wall you will see why.
If you read the manual page you will see that it uses #define _XOPEN_SOURCE before including <unistd.h>. It should actually be defined before including any header.
If you don't define _XOPEN_SOURCE then the crypt function will not be prototyped. Then the compiler doesn't know what the actual return type is, or the types and number of arguments. So it will assume that the function returns an int and your printf expects a string, so there will be a type mismatch that causes the crash.
You need this:
#define _XOPEN_SOURCE
at the top of your source file, before any #include.
Alternatively compile with the gcc option -D_XOPEN_SOURCE.
Looks like it could be related to crypto library support.
Try adding:
#include <crypt.h>
[mstanislav#pardalislabs ~]$ gcc tem.c -lcrypt
[mstanislav#pardalislabs ~]$ ./a.out
abFZSxKKdq5s6
Looks good for me!

Resources