Boost library simple compilation with tuples - c++

Can anyone tell me why this works (compiles and runs)
#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
//#include <boost/tuple.hpp>
//#include <boost/tuple_comparison.hpp>
//#include <boost/tuple_io.hpp>
using namespace boost;
int main ( )
{
// tuple<int, char, float> t(2, 'a', 0.9);
// std::cout << t << std::endl;
boost::numeric::ublas::matrix<double> m1;
return 0;
}
but when I uncomment the lines related to tuple, it says
boost_tuple.cpp:3:27: fatal error: boost/tuple.hpp: No such file or directory
compilation terminated.
I use the following to compile in both cases:
g++ -Wall -c -I/usr/include/boost boost_tuple.cpp
And I also checked that /usr/include/boost/tuple.hpp exists

If you want to include it as:
#include <boost/tuple.hpp>
lose the boost part in -I/usr/include/boost. Right now this is being evaluated as /usr/include/boost/boost/tuple.hpp, which is probably your problem.
EDIT:
Make sure tuple.hpp is installed where you said it is, because on my system it's located in another folder: /usr/include/boost/tuple/tuple.hpp
Which means that I need to include the file as #include <boost/tuple/tuple.hpp> and compile it as:
g++ tuple.cpp -o tuple -I /usr/include/boost
tuple.cpp:
#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/tuple/tuple.hpp>
using namespace boost;
int main ( )
{
tuple<int, char, float> t(2, 'a', 0.9);
boost::numeric::ublas::matrix<double> m1;
return 0;
}

Related

C++ Linking to separate .cpp file says "multiple definitions"

I'm very new to C++ and could use some help. I'm trying to link a file my_help_fxns.cpp to my main.cpp file so i can use those functions in main.cpp, but when i try linking I get the following error for each function in my_help_fxns:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Geoff\AppData\Local\Temp\ccaPL79E.o:data_vars_class.cpp:(.text+0x0): multiple definition of `my_help_fxns::print_vector_items_int_type(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<int, std::allocator<int> >)'; C:\Users\Geoff\AppData\Local\Temp\cc0mRP1w.o:main.cpp:(.text+0x0): first defined here
So it says I'm defining twice, but I don't know how to get around this. I have a class called data_vars_class. i include my_help_fxns at the top of data_vars_class.cpp, and use the helper fxns successfully in methods for that class. An instance of the class is created at the top of main.cpp. however if i try to use the helper functions in main() in main.cpp, without declaring "my_help_fxns.cpp" at the top of main.cpp, it says functions arent found, and if i do declare it at the top of main.cpp, i get the duplication error its been declared twice. How can I fix this, thanks!
this is the structure of my project
main.cpp ==>
#include "data_vars_class.hpp"
#include <iostream>
#include <chrono>
#include "my_help_fxns.cpp" <--- including here gives duplication error, but if i dont, its functions not found error
DataVars dataVars;
int main () {
my_help_fxns::pause_program();
return 0;
}
data_vars_class.hpp ==>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
class DataVars
{
private:
...
public:
...
}
data_vars_class.cpp ==>
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <array>
#include "data_vars_class.hpp"
#include "my_help_fxns.cpp"
...i can use my_help_fxns here with no problem, as an instance of this class is created before main() in main.cpp
my_help_fxns.cpp ==>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
namespace my_help_fxns
{
void pause_program() {
std::string dummy;
std::cout << "Enter to continue..." << std::endl;
std::getline(std::cin, dummy);
}
}
And here is the build command for the file in Geany:
g++ main.cpp data_vars_class.cpp -o a.out
Thanks for your help!
Don't include the my_help_fxns.cpp into the other CPP files since that will effectively define those functions in all the CPP files. This violates the one definition rule.
Instead
create a header file that declares (but not defines) those functions
include that header file in all the CPP files
add my_help_fxns.cpp to the compilation command line
Make changes to your files as described:
main.cpp
#include "data_vars_class.hpp"
#include <iostream>
#include <chrono>
#include "my_help_fxns.hpp" // change file extension from cpp -> hpp
DataVars dataVars;
int main () {
my_help_fxns::pause_program();
return 0;
}
data_vars_class
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <array>
#include "data_vars_class.hpp"
// #include "my_help_fxns.cpp" --> Not required here
And then you may simply run:
g++ -o a.out main.cpp; ./a.out
Gives here:
Enter to continue...
sdfsdfsdfsd // --- INPUT

Having one .cpp file communicate with a class.cpp and a class.h file

I don't know the correct syntax for Dice.h and Dice.cpp to talk to my final game.cpp file.
I am getting a compile error that is returning a undefined reference to Dice::Dice()
Here are my 3 file headers
Game.cpp
#include "Dice.h"
#include <iostream>
using namespace std;
int main()
{ int sum;
Dice dice1;
Dice dice2;
dice1.roll();
dice2.roll();
sum = dice1.getFace() + dice2.getFace();
cout << sum;
return 0;
}
Dice.h
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <ctime>
// definition of class Dice
class Dice //... cont
Dice.cpp
#include "Dice.h"
using namespace std;
Dice::Dice() //... cont
I get the error when i compile by typing g++ -Wall -o game game.cpp
Is this the correct way to compile multiple files?
Just because you have the includes in the files doesn't mean they will be compiled together.
Try g++ -Wall -o game Dice.cpp Game.cpp
...I could be slightly wrong on the command but that should work
Side note you should really look into Makefiles. Makes everything so much easier.
The other answer is close, but slightly off. Enter
++ -Wall -o game game.cpp Dice.cpp
Make sure that game is lower case, and I think the file containing main() needs to go first.

Linking boost with own headers and source code

I'm working with the boost libraries and opencv, and now I have to implement my own headers and source code (additional to main.cpp) that requires the libraries as well.
The main.cpp looks like (just in principle):
// STL includes
#include <stdlib.h>
...(some other STL stuff)
// Boost includes
#include <boost/array.hpp>
...(a lot of other boost stuff)
//OpenCV
#include <opencv2/opencv.hpp>
// Own header files
#include <myHeader.hpp>
main() do_some_stuff;
This works, if I don't have anything related to boost in myStuff.hpp. But if I add something in it (the function descriptions are in myStuff.cpp), like:
class aClass{
public:
aClass(int);
void doSomething(boost::shared_ptr<int>);
void doSomethingElse(cv::Mat);
};
then it says 'boost' had not been declared, or 'cv' does not name a type.
I was like, ok, I just need to include the headers in this file as well, so I added the same includes, but then when it tries to link it gives a lot of errors like:
/usr/include/boost/operators.hpp:308:35: error: expected identifier before numeric constant
/usr/include/boost/operators.hpp:308:35: error: expected ‘>’ before numeric constant
/usr/include/boost/operators.hpp:310:1: error: expected class-name before ‘{’ token
/usr/include/boost/operators.hpp:311:3: error: expected unqualified-id before numeric constant
...(a lot more of these errors)
I'm using a Makefile to build this project, that looks like:
OPENCV_I = `pkg-config --cflags opencv`
#it finds boost without any additional -I or -L options...
INCLUDEPATHS = $(OPENCV_I)\
-I.\
-L.
LIBS=-lGL -lGLU -lm -lboost_program_options -lboost_system -lboost_thread -pthread -lrt -lopencv_core -lopencv_highgui
SRCCXX := main.cpp myStuff.cpp
OBJSCXX := $(SRCCXX:%.cpp=${BUILDDIR}/%.o)
$(BUILDDIR)/%.o : %.cpp
$(CXX) $(CXXFLAGS) $(INCLUDEPATHS) -c $< -o $# -DdDOUBLE $(LIBS)
all: ${OBJSCXX}
$(CXX) $(LDFLAGS) $(INCLUDEPATHS) -o $(OUTNAME) $? -DdDOUBLE $(LIBS)
Previously I was using CMake, and it worked quite well with these kind of projects, just this one is a part of a bigger project where they use Makefiles for everything. So I guess the main problem is with the makefile, probably when I list my source codes SRCCXX := main.cpp Visualisation.cpp it doesn't like it...
Any suggestions?
Thank you in advance.
EDIT....................................
So my whole myStuff.hpp looks like:
#define _USE_MATH_DEFINES
#include <math.h>
#define RINGS 5
#define SECTIONS 12
// Boost includes
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/assign/ptr_list_of.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/date_time.hpp>
#include <boost/foreach.hpp>
#include <boost/function.hpp>
#include <boost/make_shared.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/program_options.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <opencv2/opencv.hpp>
class Sensor{
public:
Sensor(int);
void Update(boost::shared_ptr<char[RINGS][SECTIONS]>);
int Id();
private:
char data[RINGS][SECTIONS];
int id;
};
and the myStuff.cpp:
#include "myStuff.hpp"
void Sensor::Update(boost::shared_ptr< char[RINGS][SECTIONS] > buffer){
for(int i=0;i<RINGS;i++) for(int j=0;j<SECTIONS;j++) data[i][j]=buffer[i][j];
};
Sensor::Sensor(int a){
id=0;
};
int Sensor::Id(){
return id;
};
and my main.cpp:
// STL includes
#include <stdlib.h>
#include <iostream>
#include <mutex>
#include <string.h>
#include <typeinfo>
#include <queue>
#include <memory>
// Boost includes
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/assign/ptr_list_of.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/date_time.hpp>
#include <boost/foreach.hpp>
#include <boost/function.hpp>
#include <boost/make_shared.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/program_options.hpp>
#include <boost/numeric/ublas/matrix.hpp>
//OpenCV
#include <opencv2/opencv.hpp>
// Own header files
#include "myStuff.hpp"
////////////////////////////////////////// Main
int main (int argc, char **argv)
{
Sensor sensor(0);
return 0;
}
Well first things first... when including your own headers from within your project I would recommend that you use "mine.hpp" instead of <mine.hpp>. This ensures that the compiler won't search the complete include path and accidentally find some other include of the same name (different version for example).
Second, when you have a dependency within a class then you include the header for that dependency within that class. You cannot make assumptions that someone will include all your dependencies in a main class or some other class. Sometimes someone will just want to use your class by itself. You don't want them to then have to figure out your dependencies. Don't worry about replication either as the include guard (or pragma) will prevent that.
As for your particular problems you will need to give use your code. You have certainly managed to correctly include your headers at that point. I would guess that they might stem from a missing { or ; somewhere. Look at your very first error and solve that one.
EDIT
The issue appears to be with how you are using boost. I had a look at what in operators.hpp and in version 1_44 what I see is a struct definition with 4 template parameters, one of which is defaulted to boost::detail::empty_base<T>. Only thing I can say is to make sure that you have your entire boost library on your include path, and link path.
EDIT2
From your newly posted code I see a couple of problems. First is that you have WAY too many includes in your header. You should only ever have class dependency includes in your header, and always prefer to put your header files into your implementation (.cpp) file. This helps to prevent extremely long compilation times. So first modify your header to include only the dependencies you need:
#include <boost/shared_ptr.hpp>
#define RINGS 5
#define SECTIONS 12
class Sensor{
public:
Sensor(int);
void Update(boost::shared_ptr<char[RINGS][SECTIONS]>);
int Id();
private:
char data[RINGS][SECTIONS];
int id;
};
Then in your implementation the only change is to put braces around your for loops (this is for clarity and safety... understand why you put it on one line but it is not worth it). Also put your CTOR first:
#include "myStuff.hpp"
Sensor::Sensor(int a){
id=0;
};
void Sensor::Update(boost::shared_ptr< char[RINGS][SECTIONS] > buffer){
for(int i=0;i<RINGS;i++) {
for(int j=0;j<SECTIONS;j++) {
data[i][j]=buffer[i][j];
}
}
};
int Sensor::Id(){
return id;
};
Main
#include "myStuff.hpp"
int main (int argc, char **argv)
{
Sensor sensor(0);
return 0;
}
The ONLY dependency you need is boost::shared_ptr. To be honest though, I'm pretty sure you don't need that either. Anyway I would recommend that you start with the above and then build up by adding one dependency at a time.
After a couple of hours I figured it out. myStuff.cpp also requires the header inclusion. Silly mistake, but still why can't the compiler says something like non defined or can't find instead of a couple of pages of messed up errors...?
Thanks anyway for your help.

std::unordered_map not compiling with icc

Like the title says, unordered_map isn't compiling for me. I get an error saying "error: namespace "std" has no member "unordered_map""
I'm compiling with this command icc test.cpp -std=c++0x
This is the program I'm trying to compile:
#include <stdio.h>
#include <string>
int main()
{
std::unordered_map<string, int> map;
}
#include <unordered_map>
See: http://en.cppreference.com/w/cpp/container/unordered_map
Defined in header <unordered_map>

Boost precompiled headers problem

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