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
Related
I have two source files and common header file. header file is like following
#ifndef TEST
#define TEST
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <time.h>
#define PORT 5000
#define BUF_SIZE 20000
#define MAIN "GET / HTTP/1.1" //initial
#define REQUEST "GET /add?"//with query
#define SOCKET_BUFFER 2048
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#define MAIN_RETURN 2
#define CSS_RETURN 3
#define REQUEST_RETURN 4
int SET_MAIN=0;
int ads_today=0;
int process(int size,char buffer[size],char status);
int min(int a, int b);
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
#endif
I am getting error that sounds like I am including my common header file twice. common header file exists in ./headers/ directory. main function source file which contains main function is in ./ directory which is root of small project
My make file is like following
FUNCS = funcs/
HEADERS = headers/
HEADER_FROM_FUNCS_DIR=../$(HEADERS)
lh : $(FUNCS)test_func.o test.o
cc -o lh $(FUNCS)test_func.o test.o
test_func : $(FUNCS)test_func.c $(HEADERS_FROM_FUNCS_DIR)/test.h
cc -c $(FUNCS)test_func.c
test.o : test.c $(HEADERS)/test.h
cc -c test.c
clean :
rm lh $(FUNCS)test_func.o test.o
since I have defined #ifndef directive in .h file so I really don't know why my both source files including .h in lh executable which is my final program supposed to be in root of project directory result of user #: make command
project directory structure
./test.c #with main function
./headers/test.h #
./funcs/test_func.c #without main function, must be compiled first which is supporting file with functions
Can I fix this error, thanks
Also How not to hard code file names in Makefile. I really don't mind keeping names for output files but I really hate source file names in Makefile that I must hard code names of source files and header files in Makefile, Is there a way to get rid of hardcoded file names like use some sort of built-in Make flags that will help compile source files with any names and include header files
make outputs
version_1# make
cc -c -o funcs/test_func.o funcs/test_func.c
cc -c test.c
cc -o lh funcs/test_func.o test.o
/usr/bin/ld: test.o:(.bss+0x0): multiple definition of `SET_MAIN'; funcs/test_func.o:(.bss+0x0): first defined here
/usr/bin/ld: test.o:(.bss+0x4): multiple definition of `ads_today'; funcs/test_func.o:(.bss+0x4): first defined here
collect2: error: ld returned 1 exit status
make: *** [Makefile:6: lh] Error 1
Informations:
OS: Windows 10
Compiler: MinGW gcc
Language: C
SDL & OpenGL are already installed
When i try to compile the test file , i receive this error:
gcc test.c -o test
teste.c:1:10: fatal error: SDL2: No such file or directory
1 | #include <SDL2>
| ^~~~~~
compilation terminated.
The test file content is:
#include <SDL2>
#include <stdio.h>
void main()
{
printf("Testing");
}
This is my problem, help me pls.
In order to use the SDL header you need to use #include <SDL2/SDL.h> instead of #include <SDL2>.
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\"
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
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.