One header for multiple source files? - c

How might I write a single header file that defines an interface and use a separate source files to write platform-specific code?
For example:
video.h
video_windows.c
video_linux.c
video_osx.c

In your question you have all header files while you are talking about a shared header between source files.
In any case you just provide a common .h file and have 3 different
video_windows.c
video_linux.c
video_osx.c
You then include to your makefile (or whatever you use) the correct one according to the platform.
If you want to separate code in header files or in source files directly you can easily use some predefined macros, see here.

Related

C - Using .h files

I'm in the process of learning C for a coursework assignment. One thing that confuses me is header files. I've tried to find some information regarding my question to no avail.
My question is, say I have 3 different .c files. The convention is (atleast from reading sources) - each .c file has it's own .h file, e.g. parser.c has parser.h, lexer.c has lexer.h, typechecker.c has typechecker.h (if we were making a compiler).
We then go on to add a statement:
#include "parser.h"
#include "typechecker.h"
in the lexer.c file, and do the same with the other .c files (changing the header files we include).
Instead of using that convention, is it okay to add all the prototypes for all 3 classes files into one header, say header.h, and just include that in all 3 classes? The problem with this is that the 3 classes will have prototypes of functions already included in this class, but I don't see this as a problem (I'm a beginner at C so I could be wrong).
Thanks.
what you suggest is permissible but not recommended. Having all prototypes in one header will cost you in terms of compilation and building. try to concentrate on "why header files are used?". if you get this answer you will refrain from adding everything in one header file. header files are meant for modularity to provide source files only those information which they need. Secondly in large projects you have to define "private" header files which are used internally by your code and are not visible to outer word. Ofcourse you will provide other users with header file in order to use your code.
So It is not advisable to put all prototypes even in your start of learning. As starter, make one header file per source file.
EDIT
if your header1.h has function function1(), wherever(all source files) you use function1(), you will add header1.h

multiple definition of c function when use asn.1 generated source file

I have three .asn files. After these three files are compiled by the asn.1 compiler, each file has a corresponding directory in the output folder. In each folder, there are many C source files that were generated by the asn.1 compiler. There is always a .c file which has the same name as the .asn file (except the extension name, i.e. test.asn and test.c). In this file, some functions are defined; like asn_alloc, asn_free etc.
So, there are three files implementing the asn_alloc and asn_free functions. I'll use all the generated C files in my C project.
The problem is, the asn_alloc function is defined in three places. This is the reason I'm getting the compiler error:
multiple definition of function
How do I deal with this problem? I think that editing the generated source file is not a good idea.
This problem is specific to the ASN.1 Tool you are using. With some ASN.1 Tools (such as the one at http://www.oss.com), you can either include all of the .asn files in the same compilation so that one .c file is generated with all names disambiguated, or you can use the -prefix xxx option on each compilation where xxx is different for each ASN.1 compilation thus causing the generated names to be prefixed with xxx, therefore not conflicting.

C Header Files with the same declarations but different implementations

I have two sets of header files and .c files in my project i will only ever be including one of these headers but i want the option to quickly swap the header im including. Both header files have some declarations that are exactly the same but the implementations in the .c files are different. Basically what I need is way to tell the compiler to only compile the .c file that is associated with the header im including elsewhere in my program.
You could always specify the .c or .o file that you're going to link against at compile/link time for instance
gcc -o myexe file1.c/file1.o
or
gcc -o myexe file2.c/file2.o
you could even make this a different make directive if you have a makefile if you have the same header file but 2 different implementations. I would recommend just using 1 header file and changing the underlying implementation, no point in having 2 headers with similar declarations.
If both header files are exactly the same then you don't need to maintain two header files. You can keep only one copy. Whichever code includes the header file can include this single header file only.
You can always specify which .c file you want to compile while compiling. In gcc, you can mention the C file to be compiled in the command line. In Visual Studio, you can include the correct C file.
I guess you should maintain only one header file and include that in your code. Introduce a flag to the makefile to link which implementation to be linked. You have not mentioned what are you using to build.

How to tell the preprocessor to search for a particular folder for header files, when I say #include <xyz.h>

I have around 120 header files (.h files) , and in all of them each one includes many other header files using #include <abcd/xyz.h>, but as I kept .h files in a specific folder, preprocessor is generating filenotfound error.
I moved all the .h files to the single .C file that is calling the first headerfile.
One way to do is make #include <abcd/xyz.h> as #include "abcd/xyz" , but I need to do this in all the header files wherever there is an include statement, and there are hundreds of them.
I can't include many of them in the headerfiles section in Visualstudio because, some of the headerfiles have the same name, but they reside in different directories. (<abcd/xyz.h>,<efgh/xyz.h>).
Any way to do this?
You should add a path into "Additional include directories" in the "C++" section of the project options (the "General" tab). You can use environment variables as well as "this folder" (.) shortcut and "up one folder" (..) shortcut for this setting to not be bound to a certain directory structure.
and I can't include many of them in the headerfiles section in Visualstudio because , some of the headerfiles have the same name, but they reside in different directories.(,)
That's a pretty big problem unless the files that are including those non-uniquely named headers are in the same directory as the header files themselves.
You have no way to guarantee that the compiler will locate one header before another without modifying the #include directive itself (and adding a relative path as one example).
EDIT: It looks like Visual Studio will allow you to specify different Additional Include Directories for each source file in a project (rt-click on the source file in Solution Explorer and modify C/C++ properties). But I think this would be more work than modifying the #include directives themselves - depends on how many non-unique header filenames you have.
In the project settings (under C/C++ in VS2005/2008) there's an option for "additional include directories". You can add the folders containing your header files here, using relative paths.
You can also do this at the IDE level in Tools -> Options -> Projects and Solutions -> VC++ Directories -> Include Files. Typically this method is reserved for headers included as part of a formal library. The first option is typically preferred as it's portable (you can ship your project file to another developer and, provided you use relative/macro'd paths, they can build the project as-is).
What you're looking for is the -I flag and you give the directory...
If you have a Makefile, you should add it to the CPP_FLAGS something like that....
You can also add an INCLUDE variable to your environment variables.

Auto defines in C editors... Why?

When Eclipse creates a new file (.c or .h file) in a C project the editor always auto creates a #define at the top of the file like this: If the file is named 'myCFile.c' there will be a #define at the start of the file like this
#ifndef MYCFILE_C_
#define MYCFILE_C_
I have seen other editors do this as well (Codewright and SlikEdit I think).
The #defines don't seem to do anything for the editor as I can just delete them without any problem, and I can't think of a reason why I would want to use them. Does anyone know why they are there?
It's to guard against multiple definitions.
Sometimes people include a whole .c file in other .c files (or even .h files), so it has the exact same purpose of preventing an include file from getting included multiple times and the compiler spitting out multiple definition errors.
It is strange, though, that it would be the default behavior of an editor to put this in anything but a .h file. This would be a rarely needed feature.
A more modern version of this is to use:
#pragma once
It is quite unusual to see this in a .c file, normally it is in the header files only.
I think it's a throwback of C include issues, where multiple copies of the source would get included - unless you are meticulous with include chains (One file includes n others).
Checking if a symbol is defined and including only if the symbol is defined - was a way out of this.

Resources