Compiling C code with conflicting types - c

I'm coding a program that uses ossp-uuid which defines a type called uuid_t. Now, the problem that I have is that this type is already defined in Mac OSX in the file unistd.h.
So, the error I get is:
/opt/local/include/ossp/uuid.h:94: error: conflicting types for 'uuid_t'
/usr/include/unistd.h:133: error: previous declaration of 'uuid_t' was here
I complile my program with:
gcc -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -
DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\"
-DPACKAGE=\"epride\" -DVERSION=\"0.2\" -I. -I/usr/local/BerkeleyDB.4.7/include
-I/opt/local/include -I/opt/local/include/db47 -I/opt/local/include/libxml2
`pkg-config --cflags glib-2.0` -DNUM_REPLICAS=1 -DGEN_SIZE=10 -g -O2 -MT
libepride_a-conflictset.o -MD -MP -MF .deps/libepride_a-conflictset.Tpo
-c -o libepride_a-conflictset.o `test -f 'conflictset.c'
|| echo './'`conflictset.c
Is there a way to tell gcc that he should ignore the type from unistd.h? Because I'm using unistd.h for other things.
In uuid.h there is these lines:
/* workaround conflicts with system headers */
#define uuid_t __vendor_uuid_t
#define uuid_create __vendor_uuid_create
#define uuid_compare __vendor_uuid_compare
#include <sys/types.h>
#include <unistd.h>
#undef uuid_t
#undef uuid_create
#undef uuid_compare
Shouldn't that take care of it?
Thanks in advance!

You should check /opt/local/include/ossp/uuid.h at line 94 and hope that there's a define for uuid_t. Hopefully you'll find something like:
#ifndef UUID_T_DEFINED
#define UUID_T_DEFINED
typedef uuid_t .... whatever
#endif
If the guys who wrote that header did it in this way, then you can modify your code:
#include <unistd.h>
#define UUID_T_DEFINED
#include <ossp/uuid.h>
This way, he second #include won't hit the declaration of uuid_t in ossp/uuid.h.

Something like this?
#define uuid_t unistd_uuid_t
#include <unistd.h>
#undef uuid_t
#include <ossp/uuid.h> /* or whatever header you're including */
It's ugly, but well, it's C...

If you have access to the ossp-uuid library source code, then you can rename the offending identifier to something like ossp_uuid_t with simple text search-and-replace. Recompile and reinstall the library and everything should be fine.

This may be more complicated than what you need, but one option is to wrap ossp-uuid inside a shared library, and create an API that doesn't expose the underlying uuid_t type.

Related

Compiling header files in ubuntu. What do I type in terminal?

I'm pretty sure this is a simple question but I've searched online for about half an hour.
I have 3 files:
02_01.c
#include <stdio.h> // Notice the library included in the header of this file
#include <stdlib.h>
#include "myLibrary.h" // Notice that myLibrary.h uses different include syntax
#define MAX_LENGTH 21.8
#define WORK_WEEK 5
int main(void) {
function1();
return EXIT_SUCCESS;
}
myLibrary.c
void function1(void){
puts("It works :)");
}
void function2(void){
//This function does nothing as well
}
myLibrary.h
#ifndef MYLIBRARY_H_
#define MYLIBRARY_H_
void function1(void);
void function2(void);
#endif /* MYLIBRARY_H_ */
First, I navigate to my working directory.
Normally in a file with no local headers I would type:
gcc -o 02_01 02_01.c
./02_01
and it would work.
I've tried a variety of things like:
gcc -o 02_01 02_01.c myLibrary.c
which gives me an error "implicit declaration of function 'puts'
gcc -o myLibrary myLibrary.c which also gives the same error.
What should I be typing in the terminal in ubuntu?
So I'm assuming that the puts() function in myLibrary.c is not connected to 02_01.c which is where I include stdio.h.
You must include required headers in every file, where you using included functions. In your case, you must include #include <stdio.h> in beginning of your myLibrary.c file.
Also, you probably want to build .a library and link with it later.
So, finally:
Compile lib:
gcc -c -o mylib myLibrary.c
Make static lib:
ar rcs libMyLib.a mylib
Compile app and link together:
gcc -o 02_01 02_01.c -L. -lMyLib

Implicit declaration of timersub() function in Linux - what must I define?

I am trying to compile timersub() function in linux but i always get:
test.c: In function ‘main’:
test.c:27:2: warning: implicit declaration of function ‘timersub’ [-Wimplicit-function-declaration]
timersub(&now, &then, &diff);
^
/tmp/ccLzfLsl.o: In function `main':
test.c:(.text+0x55): undefined reference to `timersub'
collect2: error: ld returned 1 exit status
this is just a simple code of the function with all the library that i use..
#define _XOPEN_SOURCE
#define _POSIX_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "openflow.h"
#include "cbench.h"
#include "fakeswitch.h"
#include <unistd.h>
int main()
{
struct timeval now, then, diff;
gettimeofday(&then,NULL);
sleep(1);
gettimeofday(&now, NULL);
timersub(&now, &then, &diff);
return 0;
}
i am compiling it with:
gcc --std=c99 -Wall -DTRACE -o test test.c
See the manual. It's not in POSIX but in BSD function. So you need _BSD_SOURCE.
Define it at the top:
#define _BSD_SOURCE
or alternatively compile with:
gcc --std=c99 -Wall -DTRACE -D_BSD_SOURCE -o test test.c
Since Glibc 2.20, the macro _BSD_SOURCE has been deprecated and has been superseded by _DEFAULT_SOURCE. From feature test macros:
_DEFAULT_SOURCE (since glibc 2.19)
This macro can be defined to
ensure that the "default" definitions are provided even when the
defaults would otherwise be disabled, as happens when individual
macros are explicitly defined, or the compiler is invoked in one of
its "standard" modes (e.g., cc -std=c99). Defining _DEFAULT_SOURCE
without defining other individual macros or invoking the compiler in
one of its "stan‐ dard" modes has no effect.
The "default" definitions comprise those required by POSIX.1-2008 and
ISO C99, as well as various definitions originally derived from BSD
and System V. On glibc 2.19 and earlier, these defaults were
approximately equivalent to explicitly defining the following:
cc -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809
But if you are using an older Gblic, you'd still need to use _BSD_SOURCE.
Basically i just deleted #define _XOPEN_SOURCE and #define _POSIX_SOURCE and compiled it only with gcc -Wall -DTRACE and it worked

gcc compile multiple files

I have these five source
main.c src_print1.c src_print2.c header_print1.h header_print2.h
the contents are simple and are as following for respective files:
main.c
#include "header_print1.h"
#include "header_print2.h"
int main(int argc, char** argv) {
print1();
print2();
return 0;
}
header_print1.h
#ifndef PRINT_1
#define PRINT_1
#include <stdio.h>
#include <stdlib.h>
void print1();
#endif
header_print2.h
#ifndef PRINT_2
#define PRINT_2
#include <stdio.h>
#include <stdlib.h>
void print2();
#endif
src_print1.c
#include "header_print1.h"
void print1() {
printf("Hello 1\n");
}
src_print2.c
#include "header_print2.h"
void print2() {
printf("Hello 2\n");
}
Using gcc I have tried to compile using the following command line:
gcc -I ./ -o test -c main.c src_print1.c src_print2.c
Everything is in the same folder.
The error I get is:
gcc: cannot specify -o with -c or -S with multiple files
I looked up at gcc manual, but actually I don't understand what to do in this case, since usually I use IDE and not the command line.
IMHO, if you rewrite your compilation statement like
gcc -I./ -o test main.c src_print1.c src_print2.c
You'll be good to go. There is no need for -c flag [NOTE] when you're specifying the output binary using -o.
Also, as mentioned here, all the files are in same directory, you can even shorten the statement as
gcc -o test main.c src_print1.c src_print2.c
Suggestion: While the above change(s) will do the job, this is not considered an elegant way of doing so. Please consider creating a makefile which will make your life easier.
[Note]:
Regarding the -c option, as per the online gcc manual, (emphasis mine)
-c
Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
So, it should be clear by now, why you got the error.

C language prototype creation

I have this block of code. I have to move the given function display_name() into another .c file, compile it, and find the error that was caused due to the migration of the function and correct it by creating a header file with a prototype. How can I do it?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char student[]="Rasmus Lerdorf";
void display_name()
{
printf("Student Name : %s",student);
}
int main()
{
display_name();
}
these are the changes i made but again i still get an error in the main.cpp. it doesnt allow me to include the displayname.h file.
displayname.h
void display_name(void);
displayname.cpp
#include <stdio.h>
#include "displayname.h"
char student[] = "Rasmus Lerdorf";
void display_name()
{
printf("Student Name : %s", student);
}
main.cpp
#include <stdio.h>
#include "displayname.h"
int main()
{
display_name();
}
errors are:
3 IntelliSense: identifier "display_name" is undefined c:\Users\konstantinos\Desktop\main\main.cpp 7 2 Cproject
2 IntelliSense: cannot open source file "displayname.h" c:\Users\konstantinos\Desktop\main\main.cpp 2 1 Cproject
Error 1 error C1083: Cannot open include file: 'displayname.h': No such file or directory c:\users\konstantinos\desktop\main\main.cpp 2 1 Cproject
Prototype functions work like this: for each set of functions that you write (except main) you need a definition and an implementation. Definitions are usually stored in header files (extension .h) whereas implementations are stored in source files (extension .c).
Here is an example of how you could arrange your code to solve your problem.
Definition: display.h
// This file contains the definitions of the functions which you want to call from another file
void display_name(void);
Implementation: display.c
#include "display.h"
#include <stdio.h>
static char student[]="Rasmus Lerdorf";
void display_name()
{ printf("Student Name : %s",student);
}
With both the definition defined and the desired implemented, now you can call the function from your main source file.
Implementation: main.c
#include "display.h"
#include <stdio.h>
int main()
{
display_name();
}
This is how you link together a prototype of a function and the implementation of a function. You can expand this by adding more prototypes to display.h, implementing those prototyped functions in display.c, and then calling them throughout your code.
To build, both of these .c files must be included in your build phase. If you build from the command line, you need to do something like this (I'm assuming that your compiler is gcc):
cc display.c main.c -o program
Hope this helps.
Your header file, let's call it displayname.h should contain the declaration:
void display_name(void);
It's usually also best to create an include guard, which avoids causing problems if a header is included more than once:
#ifndef DISPLAYNAME_H
#define DISPLAYNAME_H
void display_name(void);
#endif /* DISPLAYNAME_H */
Then, in your displayname.c, you would include that header plus any others needed by the function, and define your constant and the function:
#include <stdio.h>
#include "displayname.h"
char student[]="Rasmus Lerdorf";
void display_name()
{
printf("Student Name : %s",student);
}
And in your main.c, you would also include that header:
#include "displayname.h"
int main()
{
display_name();
return 0;
}
I don't know what compiler you are using, but if you're on a Unix-like system (Linux, Mac OS X, or something like msys or Cygwin under Windows), you would compile and link them as follows (you can replace cc with your specific compiler, such as gcc or clang, though on most systems cc should exist and point to the default compiler for that system):
cc -c -o displayname.o displayname.c
cc -c -o main.o main.c
cc -o myprogram main.o displayname.o
You could also abbreviate this as:
cc -o myprogram main.c displayname.c
I also recommend, when you are learning, to use the -Wall -Wextra -Werror flags, to give you as many warnings as possible, and not allow compilation to proceed if there are any warnings. To make this more convenient, so you don't have to type the whole command every time, you can define a simple Makefile; the following uses GNU make syntax, if you don't have GNU make let me know and I'll edit it to use a more portable syntax:
CFLAGS=-Wall -Wextra -Werror
myprogram: main.o displayname.o
cc -o $# $^
%.o: %.c
cc -c $(CFLAGS) -o $# $<
main.o: displayname.h
displayname.o: displayname.h
If you have this set up, you can just type make and it will recompile everything that it needs to.
edit: I see now from your comments that you are using Visual Studio, so the above tips on how to compile and link using cc and make are not relevant to you. It has been too long since I have used Visual Studio to walk you through that myself, but Microsoft has a reasonable walkthrough of how to create and build a project that you can follow. The tutorial is for C++, but it should work similarly for C, just keep in mind that files should be named .c if they are written in C, and only .cpp if they are written in C++.
i did what you said! i created the 3 specific files displayname.h for the prototype , displayname.cpp in which the function display_name() stays and main.cpp in which i call the function display_name(). the problem again is that when i include the file displayname.h in the displayname.cpp it works fine, but when i include it in the main.cpp i have an underline error in the include. what is wrong?

Compiling using multiple headerfiles in GCC -- undefined reference to .. math

I've been working on this for a while now. I have these files:
main.c
zSim.h
zSim.c
zDynamicArray.h
zDynamicArray.c
zOptions.h
zOptions.c
zMain.h
zMain.c
All of the files and headers are located within the same folder.
My main has the following includes:
#include "zDynamicArray.h"
#include "zOptions.h"
#include "zMain.h"
#include "zSim.h"
#include <stdio.h>
#include <math.h>
I am using header guards. Just for example, this is from my zSim.h file:
#ifndef SIM_H
#define SIM_H
#include "zDynamicArray.h"
#include "zOptions.h"
//This Header is accountable
//for all Simulation Related things.
int Simulate(SIMOPT so,PRINTOPT po);
#endif
and this is a snippet from my zSim.c code (maybe I am doing something wrong here?):
#define M_PI 3.14159265358979323846
#include "zSim.h"
#include "zDynamicArray.h"
#include "zOptions.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
I am compiling using this gcc command:
gcc main.c zSim.c zOptions.c zDynamicArray.c zMain.c -o TEST
and I also used:
cc -c main.c
cc -c zSim.c
cc -c ... etc
cc *.o -o TEST
They all result in an undefined reference for anything in the math.h library. Also when I use the gcc -I command, math is still undefined.
If i compile using gcc main.c, I get unresolved references for anything in my header files.
What should I do?
try ading -lm at the end : gcc -c main.c cc -c zSim.c cc -c ... etc cc *.o -o TEST -lm
You have to link the math library with the -lm option. The reason why is explained here : Why do you have to link the math library in C?
they all result in an undefined reference for anything in the math.h library
math.h is only the header file which contains prototypes and type declarations. You also need to link against the math library when creating your executable. Add -lm to your gcc call:
gcc main.c zSim.c zOptions.c zDynamicArray.c zMain.c -o TEST -lm
See also Undefined reference to `sin`.

Resources