I try to precompile Boost headers.
First experiment - with std:: headers. I create file std.hpp:
#include <vector>
#include <iostream>
// And other std:: headers
After that:
g++ std.hpp
Copy std.hpp.gch in /usr/include/c++/4.4.5
And write test program:
#include <std.hpp>
int main() {
std::cout << "Hello, precompiled world!" << std::endl;
return 0;
}
Works fine.
Now try precompile Boost headers.
I create boost.hpp file:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
After that:
g++ boost.hpp
Copy boost.hpp.gch in /usr/local/include/boost
And write test program:
#include <boost/boost.hpp>
int main() {
// Some code...
return 0;
}
But got error:
main.cpp:2:33: error: /usr/local/include/boost/boost.hpp: No such file or directory.
Try, for experiment:
#include </usr/local/include/boost/boost.hpp>
int main() {
// Some code...
return 0;
}
Same error.
Try copy boost.hpp.gch in another place - same error.
If I put file boost.hpp in same place - works fine (so there is no problems with path):
ls /usr/local/include/boost | grep boost
boost.hpp
boost.hpp.gch
So compiler use boost.hpp header. But why compiler don't see precompiled boost.hpp.gch??
This might be a gcc bug as documented in http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46110
Related
While following the book C++ For Dummies, I have three files in my CodeBlocks project, main.cpp, Pen.h, and Pen.cpp. They look like this:
main.cpp:
#include <iostream>
#include "Pen.h"
//#include "Pen.cpp"
using namespace std;
int main()
{
Pen MyPen = Pen();
MyPen.test();
}
Pen.h:
#ifndef PEN_H_INCLUDED
#define PEN_H_INCLUDED
//#include "Pen.cpp" // Uncommenting this gives a different error
using namespace std;
class Pen
{
public:
// attributes omitted
// PROTOTYPES:
// other functions omitted
void test();
};
#endif // PEN_H_INCLUDED
Pen.cpp:
#include <iostream>
#include "Pen.h"
using namespace std;
//other function definitions omitted
void Pen::test()
{
cout << "Test successful." << endl;
}
When I run the code as listed above, I get an "undefined reference to `Pen::test()'" error. To fix this, I changed the #include statements at the top of main.cpp to:
#include <iostream>
//#include "Pen.h"
#include "Pen.cpp"
This works as intended and correctly prints out "Test successful."
My question is this: what in the world is the point of putting a function prototype in a header file if I have to import the .cpp file later on anyways?
EDIT: It turns out this was a problem with not knowing how to use Code::Blocks rather than with the C++ language.
Assuming you're using gcc, you can compile and link in one step by supplying multiple .cpp files via the command line.
g++ Pen.cpp main.cpp
clang should be similar.
clang++ Pen.cpp main.cpp
An #include should never reference a .cpp file. At all. There's no good reason to do it. Include your headers and then supply the names of all .cpp files when you compile. If your project gets big and you have too many .cpp files to reasonably list, then it's time to break out a makefile or similar.
In the main.cpp include the header file:
#include "Pen.h"
The Pen.h file it's ok.
You need to add the Pen.cpp file to the project tree.
Go to Project -> Add files... and add Pen.cpp
I would like to use C++'s std::format library to format strings. See the minimal working example below.
/* example.cpp */
#include <iostream>
#include <format>
#include <string>
int main() {
std::string s = std::format("Hello, {}!", "John");
std::cout << s << std::endl;
return 0;
}
However, when I compile my code, I get the following error message:
example.cpp:2:10: fatal error: format: No such file or directory
2 | #include <format>
I am using the latest version of macOS, and I have Homebrew installed as my package manager. I already installed clang-format through Homebrew, but for some reason, my compiler can't locate the header file. Can somebody help me figure out the problem is? I have tried using Apple's GCC and the custom GCC10 provided by Homebrew, but in both cases, I get the same error message. Is this a Homebrew issue or a C++ issue?
Following this answer to use fmt, you can install the library with brew install fmt, then modified the code to
/* example.cpp */
#include "fmt/format.h"
#include <iostream>
#include <string>
int main() {
std::string s = fmt::format("Hello, {}!", "John");
std::cout << s << std::endl;
return 0;
}
and compile with
clang++ -std=c++11 test.cpp -lfmt
I want to create a function outside the main.cpp file
i've tried creating a header file but it doesn't work:
Main.cpp:
#include "other.h"
int main() {
MyFunc();
}
Other.cpp
#include <iostream>
#include "other.h"
void MyFunc() {
std::cout << "Ohai from another .cpp file!";
std::cin.get();
}
Other.h
#include <iostream>
#include "other.cpp"
void MyFunc();
nor CPP, G++, GCC compiler work
GCC Compiling error
Errors shown by vs code
You must include a header file and not a C++ file.
And therefore, you need to remove:
#include "other.cpp"
from other.h & use the following command-line for compiling:
g++ -o output main.cpp other.cpp
You'll get it linked and then compiled, then everything should be working fine.
You must remove #include "other.cpp" in header file.
erase the line "#include "other.cpp" in your other.h and you will be fine...
Edit: you also need a header guard...
I created a C++ Console Application in Visual Studio Community 2017. There is only a main.cpp file in the project. Here is my main.cpp file:
#include <iostream>
#include "stdafx.h"
int main()
{
std::cout << "hello world!";
return 0;
}
I get a compilation error that 'cout' is not a member of std. But if I include iostream after stdafx.h, that is,
#include "stdafx.h"
#include <iostream>
int main()
{
std::cout << "hello world!";
return 0;
}
then it compiles just fine. So why does it not work when I include iostream before stdafx.h?
The answer to your question can be found, with a little puzzling, here.
stdafx.h enables precompiled headers. Based on the error given, and the discussion of how Microsoft implements precompiled headers, it seems that the compiler simply starts compiling from the include of stdafx.h forward. So when stdafx.h is placed after iostream, iostream is not included, producing the mysterious error.
here is the structure of my program:
// File: main.cpp
#include <iostream>
#include <math.h>
using namespace std;
#include "do.cpp"
int main()
{
doit();
}
// File: do.cpp
void doit()
{
cout<<sqrt(2)<<endl;
}
When I do
g++ main.cpp
Everything goes fine. Now, if I open this as an Xcode project (I have chosen "Command line utility" as project type) and try to just build and run, it keeps saying:
Use of undeclared identifier sqrt
Unknown type name 'ostream'
What should I be supposed to do? Did I do something wrong?
Thank you very much!
Matteo
Don't do this:
#include "do.cpp"
but instead put that "do.cpp" file in the same Xcode project, alongside your main.m or main.cpp file.
And when you want to build from the command line, you can do:
g++ main.cpp do.cpp -o mytesttool
which would create the command line tool named "mytesttool".
The explanation is quite simple actually. You probably added both files to the project. Xcode tries to compile each file into an object file and then link them together.
When it tries to compile do.cpp it doesn't find the definition of cout because iostream is not included and neither math.h for sqrt, as part of do.cpp.
That file compiles fine when compiled as part of main.cpp, because it is included in the file and it finds iostream and math.h and also the using declaration.
Anyway if you remove do.cpp from the project (just the reference) everything should compile as expected.
The right way without a header file
// File: main.cpp
void doit(); // declare the function
int main()
{
doit();
}
// File: do.cpp
#include <iostream>
#include <math.h>
using namespace std;
void doit()
{
cout<<sqrt(2)<<endl;
}
The right way with a header file
// File do.h
#ifndef __DO_H_
#define __DO_H_
void doit();
#endif // __DO_H_
// File: main.cpp
#include "do.h"
int main()
{
doit();
}
// File: do.cpp
#include <iostream>
#include <math.h>
#include "do.h"
using namespace std;
void doit()
{
cout<<sqrt(2)<<endl;
}
When making the new file, I forgot to de-check the "target" selection, so that when it tried to build the project it tried to build all the single files and then link them together. By disabling the "target", I got it to work.