Header files in dev-C++ - file

I'm trying to add an header file to dev-C++ but when I compile it it doesn't work.
Here are my exact steps (for my example, I'm trying to get mysql.h to work):
copy "mysql.h" into c:\dev-c++\includes
check that in dev-C++ tools > compiler options > directories > c includes and c++ includes have the path to "c:\dev-c++\includes"
include #include at the top of my file
compiled
This is what the dev-C++ compiler told me:
13 C:\Documents and Settings\Steve\Desktop\server code\setup1\main.c `mysql' undeclared (first use in this function)
As well as other errors due to not locating the header file
Are the steps I've outlined correct? Or is there something else I need to do to get the header files to compile.
P.S. I tried doing the same with VS2008 (put mysql.h in the vs2008 include folder, etc)
but still have the same error. I would like to stick with Dev-c++ if possible.

You didn't say how you included it at the top of your file. This should work if you did
#include "mysql.h"
rather than
#include <mysql>
which is a mistake that people sometimes make.
EDIT: Perhaps try using relative paths rather than an absolute path (as you seem to be doing) when specifying additional include directories? I don't know if that would make a difference (and I don't have the time to check) but I've always used relative paths and it's always worked for me (it's also good practice anyway). So, instead of
C:\Projects\ProjectName\Include
something like
\Include or ..\Include depending on your project file structure.

Dev-C++ is a port of GCC, so try this page: http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html.
Note that you probably have to tinkle with the Makefile.

I had the same problem....
You need to put the #include after "using namespace std;", in order to use your header file in the standard namespace.
For me it is working.
Best wishes.

On the left side, right click the Project and choose "Add to Project", and then select the header file.

Its very simple ...
Just make Your header file and save it as .h extension.
Then use #include "file_name.h" instead of using include
Example-
This is my header file.
#include<iostream>
using namespace std;
namespace Ritesh
{
int a;
int b;
void sum();
}
void Ritesh::sum()
{
cout<<a+b;
}
Then use of it-
#include<iostream>
#include "Ritesh.h"
using namespace std;
using namespace Ritesh;
int main()
{
a=4;b=6;
sum();
}
Output-
Output of program

Related

c language including files without directory?

to include /usr/include/test/head1.h file I should write #include "test/head1.h" but how can I just write #include "head1.h" to include this file.
The compiler looks for the usual places like /usr/include for files.
One way to achieve this if you know your compiler already looks at /usr/include for include files anyway, is the following instead:
#include "test/head1.h"
This would be a good approach if you plan to include no more files from /usr/include/test and you know no other include directory has files with similar name to avoid conflict.
Other method would be to give compiler a hint for the directory while compiling:
gcc -I/usr/include/test <other-options> yourcode.c
Have fun writing C!
The answer above mine is correct, but I just wanted to add some advice pertaining to making the #include that you want work if you're using CMake.
If you're using CMake, you can add the path up till your header in CMakeLists.txt, like so:
target_include_directories(<projectname> PUBLIC path/to/headers), and you can add as many paths as you want, separated by spaces.

C Include custom header file in Geany on Windows 10 compiling with gcc

I'm having an incredibally hard time finding answers to this for Windows. As if the majority of people use Linux...
Anyways, I need a custom CSV parsing library for C. I found one and downloaded the header file. I tried adding #include <csvparser.h> at the top of my c program but of course, it says file not found. I placed the downloaded file in the same directory as the program.
I think i need to be able to specify an absolute path in the include or place the file csvparser.h in the include directory, but I know how to do neither of these things. What is the default include directory in Windows? I use gcc as my compiler. How can i specify an absolute path in the include statement, on windows? i can find no answer to this.
Thanks!
EDIT
Thank you for the quick reply, I seem to have included the file correctly, but now I'mhaving problems using it.
I found the program at https://sourceforge.net/p/cccsvparser/wiki/Home/
I placed it in the source directory and tried using it, bbut when I try the usage example I'm getting an error. This is my code:
#include <stdio.h>
#include <string.h>
#include "csvparser.h"
#define MAXCHAR 10000
int main() {
// int i = 0;
// file, delimiter, first_line_is_header?
CsvParser *csvparser = CsvParser_new("../MagicProg/Files/MagicProg_csv_ikoria.csv", "|", 1);
return 0;
}
When I try executing this, geany gives me the error:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Geoff\AppData\Local\Temp\ccsiwJPq.o:hello.c:(.text+0x22): undefined reference to `CsvParser_new'
What am I doing wrong? thanks again
If you're including something that's in your source directory you need to use a different style:
#include "csvparser.h"
The angle-brackets form is exclusively for things found in your include path, not in your source directory. That's reserved for things like OS and compiler headers, as well as system-installed libraries.
I made the huge newb error of not including the src files along with the header file. I blame myself. thanks everyone for help

#include <x/y.h> works from one project file but not from other? need to change file configurations?

I have a some code files and a directory with some header files in sub-directories, structured like this:
code\my_file.c
code2\other_file.c
headers
where headers contains the sub-directories openssl, curl.
When I use #include <openssl/evp.h> inside my_file.c it fails with:
fatal error: openssl/evp.h: No such file or directory
However, it works from other_file.c
Additionally, when I include the full path #include "../headers/openssl/evp.h it works fine.
Is there a reason one file in my project knows to find the openssl dir and the other doesn't?
Using Eclipse
I tried changing properties, by adding an include path:
but it didn't work...
This what fixed it for me but if someone has another answer, I'd love to hear it
So, it turns out I just needed to do the exact same thing in the GNU C++ tab also:

Should I use #include <file.h> or "file.h"?

There are two ways to include a file in C :
#include <headerpath/header.h>
or
#include "headerpath/header.h"
The first one will look for the file by using a directory known by the compiler, so we can include standard files without knowing where they are.
The second way will look for the file by using only the path between quotes. (if the search fails, the compiler tries the first way instead).
We have the possibility to add one or more directories into the directories's list that the compiler know (first way). For example with gcc we have the -I option.
So at the end, these two following codes are equivalent (path_to_header is a directory) :
1)
#include "path_to_header/header.h"
int main(void)
{
return (0);
} // Compiling with : gcc main.c
2)
#include <header.h>
int main(void)
{
return (0);
} // Compiling with : gcc main.c -I path_to_header
So my questions are :
With my own header files for example, should I use the 1) or the 2) ? Why ? Maybe it's just a personal choice ? Are there different situations to know ?
Thank's for reading :)
Edit :
I'm not looking for the difference between the two ways (I think I understood them as I explained, thanks to this post), I wanted to know if there are some special situations to know about, maybe for group work or using different compilers for the same program ... Maybe I do not know how to formulate my thoughts (or it's a silly question without real answer), I have to try to know :).
For headers of the standard libraries (which probably are precompiled) use:
#include <stdio.h>
For headers of your project use:
#include "project/header.h"
Use the option -I on the command line for additional libraries.
According to the C standard the only standard difference between them is that #include <...> includes a header while #include "..." includes a source file (and falls back to the <...> behavior if no source file is found). All other differences are implementation-defined.
The distinction is important because, for example, a standard header like stdlib.h might not actually be a file, and is instead injected by the compiler at compile time.
For your own code, since you won't have such header magic, and should know exactly which source files you want included from your own work and which you want the compiler to handle (system libraries and such) you should only use <...> for includes that are not part of your project's file structure.
If your own header files are in a defined path, like the same folder with your files that use your headers you must use this way "header.h".
You must use < header.h > when the header is a system header that is not with your sources where you are including it.

variable "using" is a not a type name?

I am currently trying to create an application in C as an assignment for school, and my professor requires me to use the following to make it compatible with his compiler:
#pragma warning(disable: 4996)
#include<string>
#include<stdlib.h>
#include<time.h>
(I am using visual studio)
And for some reason #include <string> throws up a bunch of run time errors mainly consisting of the one in the title of the question. It pops up within a bunch of other dependencies ranging from cmath, cstudio, cstdlib, cstring, etc. So the bottom line is, can anyone tell me how to fix it? Thanks!
In visual studio you can get this error if you extension is '.c' VS studio will assume the file is type c and not c++ and use the c compiler and not the c++.
In the project settings under advanced options you can select compile as C++ and then files will all be compiled as C++. If you scroll down you will find the flag '-x' on the command line as added to it.
If you are using the gcc compiler this is the same as adding the following to the compiler command '-x c++'
Check the gcc reference for this flag.
The #include <string> is a C++ header file and therefore incompatible with C.
As others have suggested, you can change this to #include <string.h> so that you have a valid C header, however if your professor dictated that you use that specific set of headers, then you need to change your source file to a .ccp file instead of a .c file.
You're including C++ header file. Use #include <string.h>
You are including a C++ library in C code. The header files without any extension are C++ header files. Having ".h" extension are C header files. C++ also accepts C header files still they have no extension. They have given same names having "c" prefix and no ".h" extension.
for example
<string>
is a C++ header
<string.h>
is a C header
<cstring>
is a C header but in a C++ code.

Resources