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.
Related
#include <iostream
#include <string
#include <fstream>
using namespace std;
int main() {
string file = "mycode.h";
#include file
}
I want to include some code from another file but to user can type location of file than code to be used
This is not possible.
C++ has to be compiled to convert the source code into executable code. #include is a preprocessor command, which is executed at an early compilation stage to combine different source files into a single file that is further processed by the compiler.
The final executable file contains so-called machine-code that is specific for a CPU and an operating system, but in general it does not contain the source code (except maybe for debugging purposes).
Maybe you can solve your problem by an external control program that the user runs and that receives the include path as input, inserts it into the source code, calls the compiler, and executes the compiled executable.
For dynamic include in C++ I think you may need a C++ interpreter. There's one, called Cling:
https://root.cern/cling/
https://github.com/root-project/cling
You are asking for JIT compilation; I think CLing can work for you: https://github.com/root-project/cling
if you check the documents you will see it is doing what you are asking,
ref: https://softwareengineering.stackexchange.com/questions/29344/jit-compiler-for-c-c-and-the-likes
I'm using mongoose to build an HTTP server in C++, and I'm getting an error message when I try to include other files in my program:
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdint:183:8: error:
expected unqualified-id
using::intptr_t;
^
/Users/cs/Downloads/mongoose-master/mongoose.c:2415:18: note:
expanded from
macro 'intptr_t'
#define intptr_t long
^
This happens whenever I attempt include the following files in my program:
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <iterator>
#include <sstream>
I've tried to narrow it down to one of these files causing the problem by commenting out some of them, but it appears that any one of them causes the error. Interestingly enough, string.h does not cause the error.
It sounds like your source code contains something like this:
#include <mongoose.c>
The .c file defines a macro that collides with words used in the standard library headers.
Including a .c file is not a good practice. Instead, you should build the mongoose library, and link your program against it.
If you really have to keep everything in a single translation unit, you should be able to move that dubious include statement to after all other headers, or even to the bottom of your cpp file.
But it would be best to figure out how to build mongoose.c separately, then link against the resulting library. You can ask a separate question, or see if you get anything out of this: Can't figure out how to build C application after adding Mongoose Embedded
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.
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++.
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++).