I'm trying to use boost libraries in my C++ application. I'm trying to compile it using g++ with different options.e.g g++ -I /usr/include/boost/filesystem/ -o test.out test.cpp however it always prompt error: 'boost' has not been declared.
And here is my code:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <boost/filesystem.hpp>
using namespace std;
int main (){
string line;
string fileName = "Read.txt";
ifstream file;
string str;
file.open(fileName.c_str());
cout << "Hello, world!\n";
vector<string> fileLines;
fileLines.clear();
while (getline(file, str))
{
fileLines.push_back(line);
}
cout << "Total Line count:"<<fileLines.size()<<endl;
fileLines.clear();
cout << "Total Line count:"<<fileLines.size()<<endl;
boost::filesystem::path p("/tmp/foo.txt");
return 0;
}
I will be glad if you help me to fix this.
P.S. I'm compiling my application in Centos 4.7 and It contains Boost version 1.32 according to /usr/include/boost/version.hpp
Update:
I also commented boost instruction, but there is some problem with includes: boost/filesystem.hpp: No such file or directory.
It sounds like you have not yet installed the boost header files that you need for includes. Since you are on CentOS, you need to:
yum install boost-devel
That will place the header file you want in:
/usr/include/boost/filesystem/path.hpp
Since you are using boost::filesystem::path, you should change your #include <boost/filesystem.hpp> to #include <boost/filesystem/path.hpp>. Since -I /usr/include is passed to gcc by default, you do not need the -I /usr/include/boost/filesystem, unless you changed the include to path.hpp. However, this would be dangerous because another library may have the same header file name and then you may include the wrong header.
You can try:
g++ -std=c++11 -Os -Wall -pedantic test.cpp -lboost_system -lboost_filesystem -o test
I had the same problem
Let me know if is works
best regards,
Accodring to header files in my Centos Linux, I changed
#include <boost/filesystem.hpp>
to
#include <boost/filesystem/path.hpp>
And also compiled my program with special link options:
g++ test.cpp -o test.out -lboost_filesystem
Related
I installed VTK 7.1.1 as suggested here.
I'm working on a new project with the following code:
#include <iostream>
#include <vtk-7.1/vtkImageData.h>
#include <vtk-7.1/vtkMetaImageReader.h>
#include <vtk-7.1/vtkSmartPointer.h>
#include <vtk-7.1/vtkRenderer.h>
#include <vtk-7.1/vtkImageActor.h>
#include <vtk-7.1/vtkImageMapper3D.h>
#include <vtk-7.1/vtkRenderWindow.h>
#include <vtk-7.1/vtkRenderWindowInteractor.h>
int main()
{
vtkSmartPointer<vtkMetaImageReader> reader = vtkSmartPointer<vtkMetaImageReader>::New();
reader->SetFileName("Test.mhd");
reader->Update();
std::cout<<"Hello World";
return 0;
}
I am using the following to compile:
g++ -g -Wall -I /usr/local/include -L /usr/local/lib -o main main.cpp
However, I keep getting errors such as the following:
/usr/local/include/vtk-7.1/vtkSmartPointer.h:29: error: undefined reference to `vtkSmartPointerBase::~vtkSmartPointerBase()'
Any idea as to why? I think it might be the linking maybe?
Your assumption is correct the linker command is missing the required VTK libraries. Look in the following link for more details VTK in Makefiles.
For instance
g++ -g -Wall -I /usr/local/include -L/usr/local/lib -lvtkCommon
-lvtkFiltering -lvtkImaging -lvtkGraphics -lvtkGenericFiltering -lvtkIO
-lvtkRendering -lvtkVolumeRendering
To ease building your VTK applications you could use CMake
I am OS X user and I've recently installed "cppunit" library using brew. When I try to compile "test.cpp" file using TestCase.h header the error occurs:
> test.cpp:3:10: fatal error: 'TestCase.h' file not found
> #include "TestCase.h"
I am compiling this file:
test.cpp
#include <iostream>
#include "TestCase.h"
using namespace CppUnit;
class EmptyTest : public TestCase
{
};
int main()
{
}
Using this command:
g++ -Wall -pedantic -std=c++14 test.cpp -o test.x -lcppunit
I've also tried compiling with -I giving the path to the library directory but still with the same error.
All my friends using cppunit and brew can simply include the header and the program works fine.
I would appreciate every answer.
I've solved this problem. I had issues with Xcode. Reinstall works fine.
I get the error
"/usr/include/c++/5/cstddef:51:11: error: ‘::max_align_t’ has not been declared
using ::max_align_t;
^"
So I should update the libraries because I find this solution:
"A workaround until libraries get updated is to include <cstddef> or <stddef.h> before any headers from that library."
I wrote some command on the Ubuntu terminal such as:
bash $ sudo apt-get install apt-file
bash $ sudo apt-file update
bash $ apt-file search stddef.h
Then still the error exist.
Thank you
In the .cpp file where this compile error occurs you need to add
#include <cstddef>
before any of the other headers, e.g.
main.cpp (broken)
#include <cstdio>
int main()
{
using ::max_align_t;
puts("Hello World");
return 0;
}
Try to compile that:
$ g++ -std=c++11 -o test main.cpp
main.cpp: In function ‘int main()’:
main.cpp:5:10: error: ‘::max_align_t’ has not been declared
using ::max_align_t;
^
Then fix it:
main.cpp (fixed)
#include <cstddef>
#include <cstdio>
int main()
{
using ::max_align_t;
puts("Hello World");
return 0;
}
Compile and run it:
$ g++ -std=c++11 -o test main.cpp
$ ./test
Hello World
I compiled some code with GNU C++ 4.9 on CentOS, and the issue was not solved by ensuring top position #include (or by the older header name stddef.h).
Weird enough, I searched all header files of the compiler libraries for the global definition of max_aling_t as declared in the offending using declaration... and found none! Could it be in some 'internal compiled header?
So I simply commented-out the "using ::max_align_t;" line in the standard header (not proud of doing this indeed) and it solved the problem... and code is running...
if anyone can explain what is the meaning/impact of this max_align_t ?
I also commented-out the using ::max_align_t; line in /usr/include/c++/4.9/cstddef, while, code is running, but I don't know if there are any consequences by doing this...
I think I'm getting close, but I'm having this error I've been banging my head against the wall on for hours. I'm missing something stupid, and I've gone character by character but I can't find it.
The compiler is giving me
main.cpp:16: undefined reference to `translator::translator(std::istream&)'
collect2: error: ld returned 1 exit status
when I try to compile my program. The command I'm using to compile is:
clear && g++ -g -Wall main.cpp -o Pear
The three sections of use are as follows:
main.cpp
int main(int argc, char* argv[])
{
std::ifstream myFile;
myFile.open(argv[1]);
translator example(myFile);
myFile.close();
return 0;
}
translator.cpp
#include <fstream>
#include <iostream>
#include <string>
#include "translator.h"
translator::translator(std::istream& in)
{
table1(in);
table2(in);
}
translator.h
#ifndef TRANSLATOR
#define TRANSLATOR
#include <fstream>
#include <iostream>
#include <string>
#include "translationTable.h"
class translator
{
private:
translationTable<std::string, int> table1;
translationTable<int, std::string> table2;
translator();
public:
translator(std::istream& in);
};
#endif
Any ideas? I've tried so much, and I've looked up similar problems, but they all have different sources. Thanks in advance!
The command line for g++ needs to include both source files, like this:
g++ -g -Wall main.cpp translator.cpp -o Pear
Otherwise, the compiler has no idea from where to get the implementation of the translator::translator(std::istream&) member function.
*EDIT: * (from the comment)
I thought that basically the use of header files was so that it would know where to get each implementation of the file?
This part is grossly oversimplified, but it should help you get the picture. Recall that the process of producing an executable from C++ sources consists of two major steps - compilation and linking. The g++ program performs them both (it can do just one if you specify -c flags, or pass only .o files).
The compiler and the linker stages of g++ do not "talk" to each other directly. The compiler produces the inputs for the linker, and that's where the communication ends.
Header files are for the compiler. Specifically, they are for the first stage of compilation - the preprocessing. Once the preprocessor has finished, there is no knowledge of where the definitions came from. Even the compiler does not know it, let alone the linker. That is why you need to "help" the linker by supplying all the relevant sources to g++.
You aren't linking translator.o with your application.
g++ -g -c translator.cpp
followed by
g++ -g -Wall main.cpp translator.o -o Pear
Here's my simple first attempt at a python extension using boost. Can someone help me to understand what's causing the compilation error?
#include <iostream>
using namespace std;
void say_hello(const char* name) {
cout << "Hello " << name << "!\n";
}
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(hello)
{
def("say_hello", say_hello);
}
user#host:~$g++ main.cpp -o test.so
In file included from /usr/include/boost/python/detail/prefix.hpp:13:0, from /usr/include/boost/python/module.hpp:8, from main.cpp:8:
/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory compilation terminated.
/usr/include/boost/python/detail/wrap_python.hpp:50:23:
fatal error: pyconfig.h: No such file
or directory compilation terminated.
This line tells exactly why it doesn't work. Your compiler doesn't know where is the pyconfig.h file. You have two options here:
place pyconfig.h in a location that
g++ knows about (i.e. your
project's directory)
add -I DIRECTORY (this is capital i,
not lowercase L) flag to g++ that
will make g++ search DIRECTORY for header files
g++ -I /path/to/my/include/files main.cpp
If you face this problem in your NetBeans then just add "/usr/include/python 2.7/" folder in your NetBeans additional include option. You will get this additional include option in properties.
You need to place pyconfig.h in same directory