First things first: I'm a newbie in C/C++.
I have a library that I have to include but it has header files that use
#include <string>
I tried to include <string> but it failed. I can
#include <string.h>
though. Since it's a library I'm trying to use I can't do much about this import right ? How can I fix this problem ? Build terminates with a fatal error.
(In case that's important I'm working on Linux and genicam is the 3rd party library)
<string> is a standard C++ header. Either your compiler is broken, or installed incorrectly, or you are trying to use a C compiler on C++ code (for instance by using gcc instead of g++).
Related
I am trying to build my code using the Intel C++ Compiler, but for some reason it fails with this error:
catastrophic error: cannot open source file "stdio.h"
In this line #include <stdio.h>.
Any ideas?
stdio.h is a standard header file; it's a bad idea to have a local file of the same name. If you meant to include the standard header, it should be on your include path, and you should include it with
#include <stdio.h>
You should also consider whether you might get more benefit from including <iostream> or including <cstdio> (like including <stdio.h>, but puts the symbols safely into the std namespace).
If you're running on Windows, then installing Visual Studio, then invoking "psxevars.bat" might solve your problem, it solved it for me.
I have been trying to create an static library out of C++ code by following this tutorial. If I trying to build the project some error occurs.
#include <limits> "limits" file not found
for example.
I have been trying different build settings, e.g. C++ Standard Library with no luck.
Rename the implementation files from .cpp to .mm did not work also. Is there an workaround to solve this issues?
Try using #include <limits.h> instead of #include <limits>
I've created an Allegro 5 project in Xcode 4.6.3 as an empty project. I've added all the Allegro 5 libraries as described in the Allegro documentation. But now I need to use some C/C++ libraries and get the error, that Xcode doesn't find the libraries (e.g. 'fstream file not found').
#include <allegro5/allegro5.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_image.h>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
How can I add the standard libraries to Xcode projects so that it finds them? Unfortunatelly I can't find any solution. This is not an Objective-C Project. It's written in C++ and also works if I don't use any of these libraries.
Thanks!
Does the name of your source file end with an extension that indicates it's C++? If it ends in (for instance) .c or .m, the compiler will not consider it to be C++, therefore the C++ headers won't be found. Try changing the extension on the source file name to .cpp (or some other extension that implies C++, see C++ code file extension? .cc vs .cpp ) and see if the header is found.
I am trying to build a library in a legacy C++ project. Platform is power-pc using gcc compiler.
The compiler is giving build errors similar to this: error: string: No such file or directory or error: vector: No such file or directory.
My understanding is that it is not able to locate the standard library. Where are the standard library files typically reside, and in what format? Is it *.h or some other? I searched for this on internet but I don't think I fully understand it.
The confusing part is that another library in the same project using same source code file builds prefectly alright. This suggests to me that may be the makefiles for these two projects are different, where one IS able to locate the std lib, other isn't. But a quick comparison b/w the two didn't bring up any obvious differences. Any other thoughts on this please?
Lastly, I just learned that string.h is for c-style strings, and string is for C++ std lib. Is it ok to mix them, i.e. a source file has #include string.h, and also #include string implicitly through including some other file? I ask because this is the same situation in the file that is not building.
Thanks.
Error Message:
Compiling SOURCE_FILE.cpp
In file included from SOURCE_FILE.cpp:3:
HDR_FILE.h:1: error: string: No such file or directory
HDR_FILE.h:2: error: vector: No such file or directory
CODE IN SOURCE_FILE.cpp
#include <stdlib.h>
#include <string.h>
#include "fileABC.h"
using namespace std;
// Other code
CODE IN HRD_FILE.h
#include <string>
#include <vector>
#include <hdr.h>
#include <hdr-cseq.h>
//... OTHER FILES ETC.
If the files are not being detected as C++ source code then you can get errors like this. Are the c++ files named using an appropriate file extension?
cat test.c
#include <vector>
int main() {}
gcc test.c
test.c:1:18: error: vector: No such file or directory
The command above will compile C code, but not C++. Remember that these are two different languages. To build C++ programs you have to tell the compiler that the source is C++ one way or another. If you use g++ it will automatically assume files with '.c' extensions are C++ (at least my version does. Or you can tell the compiler to use C++ by passing -x c++ right before you pass the file. You also have to tell the linker to include the C++ library. g++ does this automatically as well, or you can pass the linker flag -lstdc++.
The hard way:
gcc -x c++ test.c -lstdc++
The easy way:
g++ test.c
In answer to your last question, you can freely mix <string.h> and <string> (though you should probably use <cstring> instead of <string.h>).
The C language string is different than the C++ std::string.
The C language string type is a sequence of characters terminated by a nul, zero, '\0', character. The function declarations for managing this data structure are in <string.h> or <cstring>, depending on whether you compile as C or C++, respectively.
The C++ std::string type is a container of characters. The actual implementation may vary among compilers, but the interface won't. The methods of the std::string are declared in <string>. The compiler must be set up for compiling in the C++ language.
The C++ std::string has methods for generating C language strings and also for creating C++ strings from C language style strings. Read up on the Standard Template Library for more information.
Also, search the Web for "C++ FAQ". This is mandatory reading before programming in the C++ language. There is also a FAQ for the C language too.
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.