let's assume I do simple function designed to be exported as DLL
#include <iostream>
__declspec(dllexport) std::string __cdecl Hello(){
return std::string("Hello world");
}
This dll is intended to be used with a program already using iostream so is it possible to do the same thing but don't include iostream ? (Maybe it's a stupid question)
If yes, how can I specify it to my compiler? (Mingw)
Each DLL would need to pass preprocessing, compilation and linking phase. DLL is the same as any other type of project (except static library). Hence, it needs all #includes, library files, and all symbols fully resolved.
If DLL need to use iostream, or any STL class for that matter - the respective code must #include appropriate header.
Each source file that uses a standard library facility needs a corresponding #include directive. It doesn't matter whether the file is built into a DLL or not.
Your file does not use any iostreams facilities, but it does use strings, so #include <string> is mandatory. If you omit #include <string> but include some other standard header, it may or may not work.
Related
how do I compile a .c or .cpp file using GCC?
I need to include some standard libraries (fstream, string, iostream) - how do I do this?
For clarification, here:
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include"ocgenerator.h";
#include"structures.h";
#include"util.h";
#include"util2.h";
using namespace std;
(my .h files are in the same directory as the src file)
If I use the command:
gcc src.cpp -o src.o
I get a lot of errors: memcpy, atoi, atol, strncmp, etc, ... "are not declared in this scope". What should I add to the command?
edit: Or is it a scope thing, and do I have to add std:: to all those functions?
memcpy and strncmp are declared in <cstring>, atoi and atol in <cstdlib>. Just include these headers to bring in their declarations.
Side notes :
No semicolon after preprocessor directives, including #include "".
No using namespace std;, especially not in headers !
(Why is "using namespace std" considered bad practice?)
Since you're building a C++ project, don't forget to link with the standard library via -lstdc++, or use g++ which forwards it to the linker for you.
Note that with GCC you don't have to prefix standard C functions with std:: (as they are also declared in the global namespace), but you should anyway for your code to be standard-compliant.
The cplusplus site helps out. If you search for a specific function, on the top you will find the necessary references for the code to compile:
http://www.cplusplus.com/reference/cstdlib/atol/
You can even check in the URL. In this case, you need 'cstdlib'.
Regarding the compiler, there are several available, g++ is a good option.
I also suggest creating a makefile for automating the building process, because it becomes an headache when your codebase grows.
For any library function you use in your code, read the man page for that function to see which header declares it, and #include that header in any source file that uses the function.
For example, man memcpy shows the memcpy(3) man page (the (3) refers to section 3 of the manual; use man 3 memcpy to specify the section).
NAME
memcpy - copy memory area
SYNOPSIS
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);
...
memcpy is part of the C standard library, and is declared in <string.h>. For C++ you can use <string.h>, but it's probably better to use <cstring>, which puts the function name in the std namespace. In general, each standard C header <foo.h> is duplicated as a C++ header <cfoo>.
I get a lot of errors: memcpy, atoi, atol, strncmp, etc,
memcpy and strncmp are declared in <cstring>. atoi and atol are <cstdlib> (or in <string.h> and <stdlib.h> if you're programming in C rather than in C++).
And be sure to use the g++ command, not the gcc command, to compile C++ source. They both invoke the same compiler (gcc compiles C++ code if the source file name ends in .cpp), but the g++ command adds some options that are needed for C++.
I've created a library, based on templates. I want to provide a file that a user of my library could just include. Because its a template library, I think the library file needs to be a .h file. So to create I included all my other template header files (with the declarations and definitions) and then compiled with g++ -E (just preprocess). The idea worked until somebody who uses the library uses a standard header (like string) or library (like boost) I've used in my library, too. Because of the preprocessing all the definitions in i.e. String got copied into the library file. If the user uses i.e. String too, the compiler throws an double definition error, because its defined in my library and in the header.
So how can I solve my problem? Is there another way of generating my library file? Or can I prevent the inclusion of the headers and just insert a include statement for everything else than my own files, which are processed at the users compiling?
Thank you
DevWurm
You should not be doing the preprocessing; you should leave that up to the machine of the end-user when they compile with your headers. All of the preprocessor work will match YOUR system if you do it, which will break, e.g. system specific preprocessing (#if defined (__APPLE__)) or limits (32 bit int). It should also be up to them to have a copy of the standard library, and they might want a different implementation of it than you have. It also reduces code bloat. Take a look at this snippet of the boost CRC header for example, which provides a templated CRC function:
#ifndef BOOST_CRC_HPP
#define BOOST_CRC_HPP
#include <boost/config.hpp> // for BOOST_STATIC_CONSTANT, etc.
#include <boost/integer.hpp> // for boost::uint_t
#include <climits> // for CHAR_BIT, etc.
#include <cstddef> // for std::size_t
#include <boost/limits.hpp> // for std::numeric_limits
Why I don't need to include cstdlib and how do I disable this? I'm using Code::Blocks with GCC compiler on Windows 7.
#include <iostream>
using std::cout;
using std::endl;
int main()
{
cout << "Hello" << endl;
system("pause");
return 0;
}
You don't need to include <cstdlib> because it (or the part of it containing system()) was included by <iostream>. It is unspecified whether or which other (standard) headers are included by standard headers. You cannot disable this behavior but should be aware of it to avoid portability problems between different standard library implementations.
You should not depend on this behavior and include <cstdlib> yourself. You should also use std::system instead of the global system. Functions from <c*> headers are only guaranteed to be in the std namespace (the global ones, on the other hand, in the <*.h> headers).
I am using MS Visual Studio 2012 and in it, <iostream> includes <istream> which includes <ostream> which includes <ios> which includes <xlocnum>. <xlocnum> includes <cstdlib>, so your program indirectly includes <cstdlib>
Although the sequence of includes might be different in other compilers and/or implementations, the reason that this code runs is that <iostream>, either directly or indirectly includes <cstdlib>.
It should be noted the the libraries that iostream includes are implementation-specific and the code might not compile in some other compiler. As a general rule, the libraries that a header file includes are not usually well-documented or part of the standards, so do not rely on indirect includes. If you need a library, include it directly and, since standard libraries are include guarded, no significant overhead will be inflicted on your program's compilation or run-time.
Suppose i am editing some large C++ source file, and i add a few lines of code that happen to use auto_ptr, like in the following example.
#include <string>
// ... (much code here)
void do_stuff()
{
std::string str("hello world");
// ... (much code here too)
std::auto_ptr<int> dummy; // MY NEW CODE
// ...
}
This example compiles on gcc 3.4.4 (cygwin), because the standard header <string> happens to include the header <memory> needed for compilation of auto_ptr. However, this doesn't work on gcc 4.5.0 (mingw); they seem to have cleaned up their header files or something.
So, when i add code that uses auto_ptr, should i immediately go look whether the file contains #include <memory> at the beginning, as this answer implies? I never do it (i find it too annoying); i always rely on the compiler to check whether any #include is missing.
Is there any option that would not be disruptive to coding, and would ensure portability of my code?
Is there a C++ standard library implementation whose headers don't include each other more than is required?
If you use something in the standard library, you should include the header in which it is defined. That is the only portable option. That way you avoid the instance you cite where one header happens to include another in one version or compiler, but not another. auto_ptr is defined in <memory>, so if you use it, include that header.
[edit...]
In answer to your comment... Are you asking if the compiler can help detect when you use something from a standard header you didn't directly include? This would be helpful, but I think it's a little too much to ask. This would require the compiler to know which standard library headers contain which standard library definitions, and then check that you included the right ones for the definitions you used.
Determining exactly how a header was included is also a tall task. If you are using a standard library definition, then you must be including the header somehow. The compiler would have to tell whether you included the header yourself (possibly through headers of your own or a third-party library) or whether it came through another standard library header. (For instance, in your example, it would have to be able to tell the difference between <memory> being included via <string> or being included within your own code.)
It would have to handle different version of the standard library (e.g. C++03 vs C++0x) and different vendors. And what if those vendors of a third-party stdlib do not exactly follow the standard, then you could get bad warnings about which headers to include.
I'm only saying this to try to explain (with my limited compiler/stdlib knowledge) why I don't think compilers have this feature. I do agree, it would be helpful, but I think the cost outweighs the benefit.
The best way is to include the correct header in which the construct is defined.
and Include files should protect against multiple inclusion through the use of macros that "guard" the files
Generally header files have "include guards" surrounding them. The guards are formed by:
MyHeader.h:
#ifndef __MY_HEADER_H__
# define __MY_HEADER_H__
//body of MyHeader.h
#endif
So, you could include MyHeader.h as many times as you want:
#include "MyHeader.h"
#include "MyHeader.h"
#include "MyHeader.h"
#include "MyHeader.h"
And it won't cause any problems for the compiler (it will only ever be included once). Moreover you could include another file that includes "MyHeader.h", and the same rule would apply.
What this means is, if you ever want to use something that is defined in a header - include it! (Even if you think something else might include it, there is no reason not to be safe).
When I create shared libraries, I have a header file (but with no file name extension) in the root of the library source named the same as the library.
So for example, if my library was called libirock.so, then I'd have a file called irock in the project root. This file will include all of the most important headers in the library, so that when the library is to be implemented, all you need to do is use this include line:
#include <irock> // Instead of <irock.h>
I got the idea from when I saw a compiler warning similar to:
#include <string.h> is obsolete, use #include <string> instead
Two questions:
Is using irock instead of irock.h best practice?
Is is correct to use a single header file instead of many headers?
Course of action
Thanks for your answers! From the answers, I've decided:
Will use <irock.h> instead of <irock>.
I will continue to use a 'primary' header file.
In a single word, no. You'll want to explicitly use irock.h
With the extension in place, it's clear that this is a header file, and any application that uses files based on file extension will correctly know how to interpret your header file.
There is nothing in the standard governing "allowed", "prohibited" or "best practices" regarding extensions for filenames.
Use whichever form you prefer. On some platforms there's a convenience factor to having an file extensions for registered types.
For what it's worth <string.h> and <string> are totally different headers. The C++ namespaced equivalent of <string.h> is actually <cstring>.
No, the <header> instead of <header.h> idiom is supposed to be for standard library (and standard template library) headers only.
#include simply puts the content of a given file name into the actual file. So if you're feeling better without file extension just do it.
Of course the file extension has a semantic meaning which is useful. Also, included files without extension are connected with the standard library in most of the users minds.
What is used in Qt4, is that you can include the file names by "class name", for example
#include <QString>
#include <QWidget>
#include <QPainter>
#include <QApplication>
#include <QCoreApplication>
Those includes are dummy includes which will include the correct header. In real life, you will see that several classes are defined in a single include.h and you included that file twice.
A reason agains using the version without the ".h" is that you will get tempted to use includes which are camelCase (or PascalNotation) and if you move the code from windows to unix (linux or mac) you will usually get into problems. Not hard to do - but you do need to take care of doing it correctly.