Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I m a new budding programmer.Is it possible for me to create a new header file of my own? Can anyone help me how to create my own header file in c++ with an example ?
Yes, of course you can, but before that you need to learn what header files are and how you can use them properly.
file: yourname.h
#ifndef YOUR_NAME_INCLUDE
#define YOUR_NAME_INCLUDE
/* Your function statement here */
#endif
yourname.cpp
#include <iostream.h>
#include "yourname.h"
/* Your function definition here */
main.cpp
#include <iostream.h>
#include "yourname.h"
/* Your function calling here */
To learn more about header files and include statements, click the link below.
Header tutorial
Yes, you can create your own header file.
Kindly go through thinking in c++ by bruce eckel vol 1.
Just to begin with, a header file is which has extension '.h'/'.hpp'
These files have declaration of user defined data structures and interfaces such has class declaration, function prototypes and etc.
After declaring and storing it into project folder. you need to include this file in .cpp/.c
eg.:
file: myheader.h
#ifndef MYHEADER
#define MYHEADER
......
#endif
file: myclass.cpp
#include <iostream.h>
#include "myheader.h"
Related
This question already has answers here:
What tokens are permitted as arguments to #include?
(2 answers)
Closed 2 years ago.
can someone explain to me why writing something like:
#include "file.txt"
#include "File.js" /* why including files other than files with .h extension is valid */
#include "anotherFile.c"
main()
{
printf("why including files other than header files (.h files) is allowed in C");
}
So can someone provide me a good tutorial online describing this. when i search online about this i find nothing. is this behavior of #include directive described somewhere in the C standard pdf or C standard html if so please guide me to the actual page where this behavior is described in the C standard with a link
The include directive is part of the C Preprocessor. The preprocessor will simply replace an include with the contents of the file referenced.
See 6.10.2 Source file inclusion:
A preprocessing directive of the form
#include "q-char-sequence" new-line causes the replacement of that directive by the entire contents of the source file identified by the
specified sequence between the " delimiters.
This question already has answers here:
Undefined symbols error when using a header file
(2 answers)
Closed 4 years ago.
I have some useful functions in another project (project 2) but do not want to copy and paste all of them into a file within the project i'm currently working on (project one). I've tried making and including a header file within project one but it hasn't worked. Do I have to copy paste project 1 into project 2? My IDE is codelite.
Thanks<3
EDIT: my header file is called hewwo.h and the code is
extern int readln(char[], int);
extern int searchstring(char[], char[]);
this file is within project 1.
and at the top of main.c in project 1 I have
#include < stdio.h> #include <stdlib.h> #include <string.h> #include "hewwo.h"
I'm trying to use the readln function in main.c and its throwing an "undefined symbols" error
The reason you're getting the error is because with the function declarations in the header file you're telling the compiler this function exists somewhere and it doesn't care where. Thus it compiles, but the links to the actual function implementations aren't there. You need to bring in the implementations via a static or dynamically linked library.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm using a makefile for C that worked when it just had main.c and data.h but then as soon as I added a function header to data.h and and file for that function which actually defined the function it started erroring at compile time.
I had:
/* main.c */
#include "data.h"
int main () { /* this is empty */ }
and
/* data.h */
char foo(char);
and
/* foo.c */
#include "data.h"
char foo(char){
...
}
The error I get is conflicting types for 'foo' and note: previous declaration of 'foo'.
Other people having this issue had tried to use a function before it was declared or something along those lines but I cant seem to find that coming up in this example.
The error message sounds like the signatures of foo in data.h is not the same as in foo.c
e.g.
char foo(char);
and
char foo(char*);
or
// missing return type may produce a different/additional warning/error, depending on the compiler
foo(char);
Check the specific locations of the conflicting declarations and compare.
You forgot your include guard in the header file. It gets included in both C files, so you get the same two function declarations pasted in. Change your header file like so.
#ifndef DATA_H
#define DATA_H
char foo(char);
#endif
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have 2 things:
A library file and headers for the same.
I cannot access the source file. Only thing I can do is use the compiled library for my application. For this I specify the header files in a main.c file.
Lets say the code in header (application.h) specified a macro TEST as below:
application.h
#define TEST
In application.c file where I am including application.h header I have:
#ifdef TEST
{
do this
}
#else
{
do something else
}
Now in my main.c file I include the header application.h
I do not intend to use the macro TEST, meaning I want to undefine it.
Obviously I can undefine in the application.h file. But I don't want to do that. Is there a way to undefine the macro in main.c
Something like this:
main.c
#include"application.h"
#undef TEST
I tried this, when I run the program it doesn't work.
Is there a way achieve this without modifying the application.h file?
As I understand your question, you want to modify main.c without touching application.c or application.h.
If application.c is including application.h, then, as far as I know, you wont be able to undef anything. application.c and main.c are different compilation units, and defines are calculated, by the preprocessor, on compilation unit level.
If there is an unconditional define in application.h, then you will have to live with it.
This question already has answers here:
What is the difference between #include <filename> and #include "filename"?
(30 answers)
Difference between angle bracket < > and double quotes " " while including header files in C++? [duplicate]
(2 answers)
Closed 9 years ago.
I would like an explanation of the difference between < header.h > and "header.h" in library #include directives. How exactly (in which locations) does the linker search for the files? In what order does it perform the search?
When we write <stdio.h> , we are referring to a header file which is available in the include directory of the system. When we write #include <stdio.h>, the preprocessor searches for the header file in the system include directory and not in the current directory. When we write #include "stdio.h", the preprocessor starts searching for this header file in the current directory and then in its parent directories. So if we write our own stdio.h, save it in the current directory, and include it in the program using #include "stdio.h" then our header will be included instead of the system header.
In short, if we use angular brackets (<>) then we are indicating that the file can be found in one if the standard directories in the
system. If we use quotation marks (" ") then we are indicating that a non-standard header is being used.