using GCC -D option - c

I did a minimal c file (main.c)
#if !defined(MBEDTLS_CONFIG_FILE)
#error "not defined"
#else
#include MBEDTLS_CONFIG_FILE
#endif
int main(void)
{
while(1);
}
now calling arm-none-eabi-gcc main.c gives error: #error "no defined" and this is OK.
But calling arm-none-eabi-gcc main.c -DMBEDTLS_CONFIG_FILE="test.h" gives error: #include expects "FILENAME" or <FILENAME> so it is defined but not to the value I expect.
What is the correct syntax ? (context: this is working from the IDE but I want to move to cmake)

It's the shell that removes the quotes from the string you pass. You need to escape the quotes to keep them in the macro:
arm-none-eabi-gcc main.c -DMBEDTLS_CONFIG_FILE=\"test.h\"

Related

C macro undeclared

I'm trying to use a header to declare some Macros for the pre-processor and use them in the code file.
That's my header : error.h
#ifndef PROJET_MODULE_H
#define PROJET_MODULE_H
#define TESTMACRO 5
#endif
and the code file : error.c
#include <error.h>
#include <stdio.h>
int main(){
printf("%d\n", TESTMACRO);
return 0;
}
And I get this error :
‘TESTMACRO’ undeclared (first use in this function)
I've tried to compile doing :
gcc error.c -o error
and
gcc error.h error.c -o error
Both gave me the error.. Any help appreciated, thanks
To sum up everything said in comment :
To include non-system headers, you have to use " and not <>
The resquest was solved changing #include <error.h> to #include "error.h"
To include System header file you can use <> or ""
To include Custom header file you should use "error.h" or "absolute path of error.h"
If you still want to include you custom header file using <> you should compile using following command.
gcc error.c -I <path of folder in which error.h resides> -o error
e.g if error.h is in /user/testuser/include/error.h then
gcc error.c -I /user/testuser/include/ -o error

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

Make .c file as a library error

I have a test.c file which contains main() function and some test cases and it cannot be modified it(such as adding "include *.h"). Then I have a foo.c file which contains some functions(no main() function). These functions will be tested through test cases in test.c file. What I'm going to do is use foo.c as a library and link it to test.c file. And here is the simple code.
test.c
//cannot modify
int main(){
...
bar();
...
}
foo.c
#include "foo.h" //I will explain this below.
int bar(){
...
}
I'm trying to implement an interface using .h file, such as
foo.h
#ifndef _FOO_H_
#define _FOO_H_
extern int bar();
#endif
Then using cmd line
gcc -c foo.c
gcc -o output test.c foo.o
./output
You may guess the result. There is a warning that "implicit declaration of function 'bar' is invalid in C99 [-Wimplicit-function-declaration]". And the test.c file cannot run correctly.
Could someone help me about this? Thank you so much!
Your problem is:
test.c has a call to bar() in it.
test.c doesn't have any declaration for bar, nor does it have an #include for a .h file that declares bar.
You are not allowed to change test.c in any way to add either a declaration or an #include.
This is a hard problem. The C language requires there be a prototype/declaration for bar in test.c! It can be written directly in the test.c file (write extern int bar(); before you call it), or the declaration can come in from another file with an #include statement, but you must have it.
Luckily, GCC has a way to force an #include statement into a file while it's compiling the file. You don't have to change test.c in order to make it start with #include "foo.h". This will solve your problem:
gcc -c -include foo.h test.c
You need to include the declaration of bar in the test.c file:
#include "foo.h"
So that the compiler have the prototype in the translation unit, of test.c.

How to use M_LN2 from Math.h

I am trying to use the Constant M_LN2 from the math.h library but always seem to get a compiler error. The code is:
#include <stdio.h>
#include <math.h>
int main(){
double x = M_LN2;
printf("%e",x);
return 0;
}
compiling with gcc on ubuntu
gcc -std=c99 lntester.c -o lntester -lm
getting output:
error: 'M_LN2' undeclared
any help in understanding why this is happening would be greatly appreciated.
As stated below the if def were not getting defined and using gcc and c99 were causing the issue. Below is the compile code that solved the issue and allowed me to use c99.
gcc -std=c99 -D_GNU_SOURCE lntested.c -o lntester -lm
any help in understanding why this is happening would be greatly
appreciated.
You can open /usr/include/math.h and try to found definition of M_LN2. For me it is defined and wrapped by condition macros:
#if defined __USE_BSD || defined __USE_XOPEN
...
# define M_LN2 0.69314718055994530942 /* log_e 2 */
...
#endif
When you compile your code with option -std=c99 neither __USE_BSD no __USE_XOPEN defined, so all variables which wrapped by if define not defined too.
You can compile your code without -std=c99 option or with -std=gnu99 option instead.

Compiling C code with conflicting types

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.

Resources