473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how can I create a header file ?

I've found thinks on this in other places, But even when I used premade headers from my compiler ( Dev C++) which should be called Devil C++ in my opinion, and replaced all the code from one and altered it to be in C ( as to my knowledge) I keep on getting an error or 2 ( I've seen others talk about this error though). can you guys walk me through making my own headers( if i learn how to do non standard based C headers I might try and make a mini program that I think could work as an OS, And yes I've tried assembly language before ( I think I can ask my uncle as one of them has done robotics). the errors I get are

[Linker error] undefined reference to `WinMain@16'
ld returned 1 exit status
C:\Users\roddy\Documents\RAD Studio\7.0\Demos\CPP\Apps\ImageProc\Makefile.win [Build Error] [Project1.exe] Error 1

my code is:
random.c
Expand|Select|Wrap|Line Numbers
  1. #include <math.h>
  2. #include "random.h"
  3. double xrt( unsigned long long i, unsigned long long y){
  4. double x = 0.0;
  5. x=pow(y,1/i);
  6. return x;
  7. }
  8. double log_x( unsigned long long c, unsigned long long p){
  9. double x = 0.0;
  10. x=log(c)/log(p);
  11. return x;
  12. }
and

Expand|Select|Wrap|Line Numbers
  1. //---------------------------------------------------------------------------
  2.  
  3. #ifndef randomH
  4. #define randomH
  5. //---------------------------------------------------------------------------
  6. #include <math.h>
  7.  
  8. #endif
  9.  
  10. double xrt( unsigned long long i, unsigned long long y);
  11. double log_x(unsigned long long c,unsigned long long p);
the original header and source I took some of this from was ABOUT.h,About.c I believe.
Nov 28 '09 #1
38 6136
weaknessforcats
9,208 Expert Mod 8TB
There is nothing special about header files. They are just text files. They are only called header files because usually they are included at the beginning (or head) of the implementation file.

The .h extension is not required but it customary in C. C++ standard header files have no extension.
You do need to be careful of this:

Expand|Select|Wrap|Line Numbers
  1. #include "myheader.h"
This tells the preprocesspr to first look for the header file in the same directory as the source file. If not there, then look in the "standard places".

Whereas this:

Expand|Select|Wrap|Line Numbers
  1. #include <myheader.h>
tells the preprocesssor to look only in the "standard places" for the header file.

A "standard place" is a predefined path. Consult your compiler documentation for instructions on how to specify a standard place.


Next, header files need to be able to include themselves without causing re-definition errors. You need to so this:

Expand|Select|Wrap|Line Numbers
  1. #ifndef AUNIQUETAG
  2. #define AUNIQUETAG
  3.  
  4. ...contents of the header file go here...
  5.  
  6. #endif
This is called an inclusion guard. It only comes into placy if the header file is included more than once in the same source file.

And that's all there is to it.
Nov 28 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
On looking at your post. I see a missing reference to WinMain16.

That tells me: a) you are using Visual Studio and b) you have incorrectly set up your project.

What you have is a Windows project to produce a Windows program. Probably you wanted a console application so you could use main().

If so do this:

1) Create a Win32 project.
2) When the wizard appears DO NOT CLICK FINISH.
3) Instead click Application Settings
4) Select Console Application and Empty Project
5) Now click Finish.

TheWinMain16 error will go away.
Nov 28 '09 #3
I obviously am missing something things I've tried are:

using compilers headers as a template,
copying a code said to be accurate,

both of these gave me the same result then i took something out that I thought was just for the example and I got more errors put it back in exactly as I remembered it and got even more errors I think.
Nov 28 '09 #4
a is completely wrong and b is likely as I've never done this before.
Nov 28 '09 #5
not to be mean but it would be nice if you read my first post it clearly makes fun of the fact I'm using Dev C++
Nov 28 '09 #6
Banfa
9,065 Expert Mod 8TB
So there is no need to use the standard headers as temlates, just include them as required and create you own headers with your own declarations (and definitions where appropriate).

weaknessforcats is just overlooking the fact you can actually make Windows programs in a variety of IDEs and assumed you have used the most common one. However the point is that you told the toolset you where building a Windows program so it expected your code to contain a WinMain and it didn't. The normally cause for this is that the user meant to set up a console application project but set up a Windows application project so their code contains main not WinMain. However I see no evidence of your program containing either.

Talking of Dev-C++ that IDE is getting rather long in the tooth now, you should consider moving onto a more up to date IDE with a more up to date compiler, ones you can get for free are Code:Blocks or Visual Studio 2008 Express Edition.
Nov 28 '09 #7
I still don't know how to make the project type to make the headers why care about a compiler change if I can't figure out what project type i need for the job in dev c++ I don't see anything close to anything that could be a header setup I know I need both the header and a source code I don't see how to do that.
Nov 28 '09 #8
Banfa
9,065 Expert Mod 8TB
That's easy, if you learn what you need to do in Dev-C++ then change IDE you will need to learn it again because how projects get set-up is IDE dependent they all have their own slightly different ways. Change first and then learn and you only have to do the job once.

There is no header set-up, a few IDEs (Eclipse does for example) have a create header file option but most don't. It is just a matter of creating a new file and giving it an appropriate extension.

The project is about setting the appropriate options to the compiler and linker, nothing to do with the source code, all the source files, header files (h) and code files (c or cpp) are just simple text files that should easily transfer between different IDEs.

You appear to be trying to treat 2 jobs as one, the 2 jobs you have are

1. Creating a project with the right settings
2. Creating you header file (and other source code files)

You seem to be looking for a create a project that creates a header file option but that just doesn't exist because it doesn't make sense, most projects contain multiple header files, I regularly use 1 header file for every code file plus a few extra header files.
Nov 28 '09 #9
I'm less than 20 minute away from a trial of visual studio professional so how do I do it in that one ? so I use source file creation ?
Nov 28 '09 #10
Banfa
9,065 Expert Mod 8TB
Trying to remember, its something like

File -> New -> Project

Then in the box that appears select console application, it will give you the option of an empty project or a "Hello World" project. Which you choose is your choice the "Hello World" creates a few files and puts a "Hello World" program in them, the empty project doesn't. Or see the instructions in post #3.

Having got the project you can create new files by right clicking on the project in the Solution Explorer (the bit with the file list) and selecting Add -> New Item. If you have existing source files you can add the to the project by right clicking on the project and selecting Add -> Existing Item.
Nov 28 '09 #11
sounds like the same thing I'm doing already
Nov 28 '09 #12
Banfa
9,065 Expert Mod 8TB
Very possibly, I didn't say you would be doing anything much different only that you shouldn't use an out of date IDE (and toolset). There are only so many ways to add a header file to a project.
Nov 29 '09 #13
I did everything I can think of to fix errors here's my new list:

Warning 1 warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data c:\users\roddy\documents\visual studio 2008\projects\roddy\roddy\roddy.c 6 roddy

Error 2 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib roddy

Error 3 fatal error LNK1120: 1 unresolved externals C:\Users\roddy\Documents\Visual Studio 2008\Projects\roddy\Debug\roddy.exe roddy
Nov 29 '09 #14
weaknessforcats
9,208 Expert Mod 8TB
The first error is converting a double (8 bytes) to a float (4 bytes) and that may mean a loss of accuracy. The most common cause is:

Expand|Select|Wrap|Line Numbers
  1. float data = 1.0;
because 1.0 is a double. You need:

Expand|Select|Wrap|Line Numbers
  1. float data = 1.0f;
You see this also in calling a function with a float argument using a symbolic constant like 1.0.

The second error is lack of a main() function in your program.
Nov 29 '09 #15
Expand|Select|Wrap|Line Numbers
  1. #ifndef RODDYH
  2. #define RODDYH
  3.  
  4. float log_x(unsigned long long a, unsigned long long b);
  5. float root_x(unsigned long long c,unsigned long long d);
  6.  
  7. #endif
and

Expand|Select|Wrap|Line Numbers
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include "roddy.h"
  4.  
  5. float log_x(unsigned long long a, unsigned long long b)
  6. {
  7. float c = 1/b;
  8. return pow(a,c);
  9. }
Warning 1 warning C4244: 'initializing' : conversion from 'unsigned __int64' to 'float', possible loss of data c:\users\roddy\documents\visual studio 2008\projects\roddy\roddy\roddy.c 7 roddy

Warning 2 warning C4244: 'function' : conversion from 'unsigned __int64' to 'double', possible loss of data c:\users\roddy\documents\visual studio 2008\projects\roddy\roddy\roddy.c 8 roddy

Warning 3 warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data c:\users\roddy\documents\visual studio 2008\projects\roddy\roddy\roddy.c 8 roddy

Error 4 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib roddy

Error 5 fatal error LNK1120: 1 unresolved externals C:\Users\roddy\Documents\Visual Studio 2008\Projects\roddy\Debug\roddy.exe roddy
Nov 29 '09 #16
Banfa
9,065 Expert Mod 8TB
Warning 1,2 accessing a and b
On line 7 and 8 of you cpp file you are mixing integer and floating point types in your arithmetic. The compiler automatically converts the integer types to floating point types but issues a warning when doings so.

Warning 3 returning pow
pow returns double but you return float, another automatic convertion is done but a warning is issued.

The function probably does not do what you expect because 1/b when b is an integer type is unlikely to do what you think it does. That is because all the components of 1/b are integers so it is done in integer arithmetic, that is the result is given as an integer. So you might think the result of say 1/2 would be 0.5 but 0.5 is not an integer, the fractional part of 0.5 is dropped to give a result of 0. In fact under integer arithmetic 1/b can only have 4 results

b=0 gives divied by zero error
b = 1 gives 1/1 = 1
b = -1 gives 1/-1 = -1
b = any other value 1/b is fractional and the result is 0.

Error 4 - you are aware that you need to write a main function as the entry point of your program are you?

Error 5 - you can ignore it is just re-reporting error 4 as a summary.
Nov 29 '09 #17
working with .c file not .cpp, header never used main that I have ever seen.
Nov 29 '09 #18
Expand|Select|Wrap|Line Numbers
  1. #ifndef RODDYH
  2. #define RODDYH
  3.  
  4. double log_x(double a, double b);
  5.  
  6. #endif
Expand|Select|Wrap|Line Numbers
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include "roddy.h"
  4.  
  5. void main();
  6. double log_x(double a, double b)
  7. {
  8. double c = 1.0/b;
  9. return pow(a,c);
  10. }
still got both errors
Nov 29 '09 #19
Banfa
9,065 Expert Mod 8TB
First main returns int it should be

int main()

Secondly, you have to define a function called main. All you have done is declared one (See Below). The linker expects to find a main function defined in your program it is the program entry point.

Read entry point

and main

You will never find main in any header file because it is not a function you call (normally) so it doesn't need to be declared for you. It is a function that the C start-up code created by the linker calls and it already knows how it is declared, you have to define this function in one of your C files.





Note From Above Difference between Declaring and Defining
When you declare something all you do is say it exists somewhere, a function declaration looks like this
Expand|Select|Wrap|Line Numbers
  1. void fn(int q);
When you define something you not only say it exists but also give its actual contents. In the case of a variable that is an instruction to the compiler to actually assign memory to it, for a function you write the block of code that is executed by the function. This is a function definition
Expand|Select|Wrap|Line Numbers
  1. void fn(int q)
  2. {
  3.     while(q-- > 0)
  4.     {
  5.         putchar('*');
  6.     }
  7.  
  8.     putchar('\n');
  9. }
  10.  
Nov 30 '09 #20
main is in most programs but this is header and function set I usually let dev c++ make main for me
Nov 30 '09 #21
Banfa
9,065 Expert Mod 8TB
main is in ALL or its equivalent if the platform has redefined the entry point, for example WinMain in a Windows program.

If you are trying to create a function or set of functions that are to be used by including them into a different program then you have created the wrong type of project. You have created a console application project but you should have created a library project if you wanted to write functions without writing a program.
Nov 30 '09 #22
The thing I hate is that I asked a question about header files, got told by you to switch IDE's and to use a console app yet now I'm being told that's wrong. also the only thing I see that says library is class library and I'm not trying to make a class.
Nov 30 '09 #23
Banfa
9,065 Expert Mod 8TB
I did not tell you to switch IDEs I suggested it might be worth doing as an side to a post in which I was trying to answer your question.

We told you to use a console application because up until post 21 you can no indication that you were attempting to create a library, you appear to be attempting to write a program without main or with the wrong project type so we had to guess what project type you required which we not wrong in no small part due to the fact that at no time have you every properly said what it is you were trying to achieve.

Class library is not what you want, if you followed the instructions given to you in post #3 and bother to examine the displayed dialogues you would see the options is for creating a DLL (dynamic link library) or a static library.
Nov 30 '09 #24
no you know the fact that the question involved the word header is no indication, telling me to consider switching is in a way telling me to so in fact you have told me to switch, I've followed #3 then made the files inside, and surprise I got exactly the same errors!
Nov 30 '09 #25
weaknessforcats
9,208 Expert Mod 8TB
The compiler is only reporting what it finds. Any errors are in the code. You fix the first error only. Then rebuild. Then fix the first error only and rebuild. Continue this process until you have no errors.

It is irrelvant what you think or believe as the compiler is completely impersonal. But until that first error is fixed you will be dead in the water. Pay cloase attention to Banfa as he knows at lot about this stuff.

BTW: Header files do not need to be part of a project. It is the #include directive that prompts the preprocessor to locate and include your headers. You need header in a project only if that project is the one that maintains the headers.

BTW: It doesn't matter that the file extension is .c or .cpp. That's just a convenience for you. Most IDEs can compile the all the files a C or C++ based on a project setting. The .c for C code or .cpp for C++ code is just a default.
Nov 30 '09 #26
fine I'll redefine what I want:

1) a header file
2) the source to go along with it so that the functions have meaning and can be later included in projects
Nov 30 '09 #27
<code removed>

like this
Nov 30 '09 #28
Banfa
9,065 Expert Mod 8TB
Err, sorry I had to remove that code because it contained a copyright notice.

However everything you need to achieve what you want in post #27 has already been posted here.

Post #2, weaknessforcats tells you how to create a header file,
Post #3 they tell you how to create a project for that header file and your source code in the IDE you are now using.

I have told you that you need to modify the instructions in post #3 to create a library (either static or DLL) instead of a console application, it is just a different radio button on the same applications settings dialogue.


Thought out the whole of this thread I confess to being a little perplexed as to why you are making the task of creating a header file, which is a plain text file, so complex.
Dec 1 '09 #29
okay I got the dll built but now it's asking me to name an exe to debug I don't have one that uses the new codes how do I do this part.
Dec 1 '09 #30
Banfa
9,065 Expert Mod 8TB
That is because neither a DLL nor a static library are programs so they are not executable.

A DLL is a loadable image but it is loaded by a program. If you want to test your functions and debug them you will need to create a program that calls them.

Look before you go any further how do you intend to achieve "can be later included in projects"? Do you intend to take the source files and just compile and link them into your later projects or do you intend to create a library with them and then link your later projects with this library.

It makes a difference to what you do now.

If you are going to create a library that you will link later projects with then you are on the right tracks already. To test your code you will need to create an application to call the library (it can be another project in the same workspace).

If you intend to just compile and link the source code into future projects then there is little point you creating a library now as it would just result in you needing multiple projects as above. You may as well have a program that includes both your functions and the test code and main. Now unfortunately that would mean changing your project type again but the reason we have directed you to use a library project is that you have been resisting adding a main function to your code in order to make it a program. If you are going to do this we would of course advise putting the main function into a separate c file.
Dec 1 '09 #31
I intend include "________.h"
Dec 1 '09 #32
Banfa
9,065 Expert Mod 8TB
Including the .h file only gets the declarations into your future project.

Like I say you seem to be very focused on the header source file but the actual code in in the code source file, the ____.c, that is where your functions are defined and without that code compiled and linked in your future project all that will happen when you include the _____.h and call the function is you will get a linker error along the lines of "unresolved external symbol" because you will have said that the symbol exists in the header file but you will not have actually defined it anywhere.


You choices for getting the the code (___.c) into your project are
  • Compile and link the source code into a library. Link your future project with the library.
  • Compile the source code into an object now. Supply the object to your future project as and external object for the future project to include when linking.
  • Supply the future project with the ____.c so that it can be included into the project and compiled with the rest of the project source code.
In all 3 cases you also supply the ____.h to the future project as well.

The point is that the ____.h just contains the information required to know how to call the function, it does not contain the function itself that is in the ____.c but you seem to have lost sight or be unaware of that.
Dec 1 '09 #33
should I make my own exe to test with as it doesn't work with the one provided to me as an option
Dec 2 '09 #34
Banfa
9,065 Expert Mod 8TB
Yes and no, first you should decide how you intend to delieve the code to the future project.

Although lumping everything into a simple little console application always gives you the opeion of creating a library later.
Dec 2 '09 #35
basically I want it such that include "________.h calls the source instead of including the source file within the codes compiled with the project.
Dec 2 '09 #36
Banfa
9,065 Expert Mod 8TB
The _____.h has nothing to do with the object code produced by compiling the source. You can not include a function into you future project just by including the header, you have to do one of
  1. Link against a library created by compiling the source
  2. Link against an object file created by compiling the source
  3. Include the source file into the future project directly for it to compile itself

Putting source code (function definitions) into header file is a source route to either disaster or massive code bloat.

You have no choice about that you have to choose 1 of those 3 routes, there is no route where you can just have a header file. If there was there would be no need for the ____.c file at all in which case there would be no need for the function definition which implies that the compiler can create the code for your function given just the prototype which implies the compiler can create all code without any programmer input which is clearly nonsense.
Dec 2 '09 #37
when I debug it was asking for an exe to debug with I chose the one it allowed me to without typing but that doesn't work. How do I change it now ? second unless I have a code back to the header how am I going to test the code ? so do I connect it to the exe that I've now made then change the code to point towards the header and act on the codes ?
Dec 2 '09 #38
Banfa
9,065 Expert Mod 8TB
This is because you created a DLL (dynamic link library) when you made a project that created a library (as opposed to a static library). The DLL needs to be launched by an EXE so the IDE needs an executable to use to in the debugger that will launch the DLL. Using the default program will not work because the default program neither calls nor links with the DLL so it doesn't launch the DLL and even if it did it doesn't call your function so there is no way to test it.

You could set-up a project for a test executable that links against the DLL you created and calls your function. However that is quite a lot of work if you do not intend to release the code as a DLL to the future project which is why I keep asking you how you intend to release your code to the future project so that you can make a sensible decision about how to go about testing and debug of your function now.
Dec 3 '09 #39

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Christian Giordano | last post by:
Hi guys, does anyone knows which are the functions I've to use to create a file, in this case a txt or a csv, that will be asked in runtime to the user to download it? So I suppose without creating...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
0
by: nail | last post by:
Hi. I have this xml file: <menu> <header caption="File"> <item header="File" caption="New File"></item> <item header="File" caption="Open File"></item> <item header="File" caption="Close...
18
by: Al | last post by:
I'm still trying to do this but it never worked! In a .cpp file, I write the code, and at the beginning, I write: #ifndef MYLIST_H #define MYLIST_H ....to end: #endif What's wrong with it for...
3
by: christiang | last post by:
Hi guys, I'd like to use Alexia thumbnail service. It's an interesting service, you pass an url to its cgi and you get a thumbnail of that url. Of course they don't have the screenshots of all the...
6
by: windandwaves | last post by:
Hi Folk Some of my clients asked me to create "fancy emails" for them (aka html formatted emails). I know how to make a nice html document, but I had trouble creating a simple way to provide...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.