C++: Armadillo does not want to cooperate with hdf5 format - c++

Recently I have decided to store my data into hdf5 binary instead of ASCII files. I would like to use hdf5 format. Basically the thought is have the header and the data in the same file (header ASCII not binary format and then binary format). Something like this:
----------------------------------------
Dataname : testdata
ref_ell : wgs84
bmin :
etc.
and here are the data in hdf5 format
The armadillo library (http://arma.sourceforge.net/docs.html#save_load_mat) do have the function to append data to the existing file (hdf5_opts::append). But I have reached the problem much sooner. I have followed the manual but apparently I did something wrong. Lets say I have:
#include <iomanip>
#include <iostream>
#include <fstream>
#include <map>
#include <cmath>
#include <algorithm>
#include <vector>
#define ARMA_USE_HDF5
#include <hdf5.h>
#include <armadillo>
// g++ -O3 -lhdf5 -larmadillo -DARMA_DONT_USE_WRAPPER -DARMA_USE_BLAS -DARMA_USE_LAPACK -DARMA_USE_HDF5 - hdf5.cpp -o hdf5.o
// g++ -O3 -lhdf5 -larmadillo hdf5.cpp -o hdf5.o
// g++ -O3 -larmadillo -lhdf5 hdf5.cpp -o hdf5.o
using namespace std;
int main() {
arma::mat amat = arma::randu<arma::mat>(5,6);
cout << amat << endl;
amat.save( arma::hdf5_name("A.h5", "my_data"));
arma::mat bmat;
bool t = bmat.load( arma::hdf5_name("A.h5", "my_data"));
cout << bmat << endl;
if(t == false)
cout << "problem with loading" << endl;
return 0;
}
I tried to compile this exercise but I get only errors:
Either this:
hdf5.cpp: In function ‘int main()’:
hdf5.cpp:28:43: error: ‘hdf5_name’ was not declared in this scope
amat.save( hdf5_name("A.h5", "my_data"));
Or:
g++ -O3 -lhdf5 -larmadillo hdf5.cpp -o hdf5.o
hdf5.cpp: In function ‘int main()’:
hdf5.cpp:27:16: error: ‘hdf5_name’ is not a member of ‘arma’
amat.save( arma::hdf5_name("A.h5", "my_data"), arma::hdf5_binary);
What am I missing? (Solved - an update of the armadillo lib was required !)
Proceeding to second part of the problem: To save the header first and then add the data in hdf5 format. This way it works. But the header is added after the matrix is stored.
#include <iomanip>
#include <iostream>
#include <fstream>
#include <map>
#include <cmath>
#include <algorithm>
#include <vector>
#define ARMA_USE_HDF5
#define ARMA_DONT_USE_WRAPPER
#include <hdf5.h>
#include <armadillo>
// g++ -O3 -larmadillo -lhdf5 hdf5.cpp -o hdf5.o
using namespace std;
int main() {
arma::mat amat = arma::randu<arma::mat>(5,6);
cout << amat << endl;
amat.save( arma::hdf5_name("A.hdf5", "gmodel", arma::hdf5_opts::append ) );
ofstream f_out; f_out.open( "A.hdf5", ios::app );
f_out << "\nbegin_of_head ================================================\n";
f_out << "model name : " << "model_name" << endl;
f_out << "model type : " << "model_type" << endl;
f_out << "units : " << "units" << endl;
f_out << "ref_ell : " << "ref_ell" << endl;
f_out << "ISG format = " << "isg_format" << endl;;
f_out << "end_of_head ==================================================\n";
f_out.close();
return 0;
}
When i switch the order, the amat.save() function just rewrites the content of the A.hdf5 file.

For me the code worked (in Ubuntu 17.10) using
g++ hdf5.cpp `pkg-config --cflags --libs hdf5` -DARMA_DONT_USE_WRAPPER -I/home/claes/armadillo-8.500/include -o hdf5.o -lblas -llapack
where
`pkg-config --cflags --libs hdf5`
expands to
-I/usr/include/hdf5/serial -L/usr/lib/x86_64-linux-gnu/hdf5/serial -lhdf5

Related

C++ - Compile and link multiple files

I have a project with the following structure:
Item.cpp
Item.h
main.cpp
Makefile
The following source code is in the Item.h file:
class Item {
public:
Item();
~Item();
};
The following source code is in the Item.cpp file:
#include <iostream>
#include "Item.h"
Item::Item() {
std::cout << "Item created..." << std::endl;
}
Item::~Item() {
std::cout << "Item destroyed..." << std::endl;
}
The following source code is the content of the main.cpp file:
#include "Item.h"
#include <iostream>
int main() {
std::cout << "Initialize program..." << std::endl;
Item item_1();
std::cout << "Hello world!" << std::endl;
return 0;
}
And finally, the following source code is the Makefile file:
CXX = g++
all: main item
$(CXX) -o sales.o main.o Item.o
main:
$(CXX) -c main.cpp
item:
$(CXX) -c Item.cpp
clean:
rm -rf *.o
When I run the make command and then I run the compiled code with the command ./sales.o, I get the following output:
Initialize program...
Hello world!
Why is the output of the constructor method of the class Item not printed in the console? I found in some web pages that you can compile the source codes in steps and then you can link it with the -o option when using g++ but it does not work in this case. How can I compile this source codes step by step and then link it in the Makefile?
I'm surely you ignored this warning :
warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
#include "Item.h"
#include <iostream>
int main() {
std::cout << "Initialize program..." << std::endl;
Item item_1;
std::cout << "Hello world!" << std::endl;
return 0;
}
just remove parentheses it will be work
test : https://godbolt.org/z/KrdrhvsrW

this_thread / chrono has not been declared

I have checked many StackOverflow posts, but no answers solve my problem.
I get 2 errors:
g++ .\main.cpp -fopenmp -o test
.\main.cpp:12:14: error: 'std::this_thread' has not been declared
12 | std::this_thread::sleep_for(chrono::seconds(20000) );
.\main.cpp:12:37: error: 'chrono' has not been declared
12 | std::this_thread::sleep_for(chrono::seconds(20000) );
My current G++ version is:
g++.exe (MinGW.org GCC Build-2) 9.2.0
The code is:
#include <iostream>
#include <chrono>
#include <thread>
#include <omp.h>
int main()
{
omp_set_num_threads(4);
#pragma omp parallel
{
std::this_thread::sleep_for(chrono::seconds(20000) );
std::cout << "Number of available threads: " << omp_get_num_threads() << std::endl;
std::cout << "Current thread number: " << omp_get_thread_num() << std::endl;
std::cout << "Hello, World!" << std::endl;
}
return 0;
}
I have already tried -std=c++11 from 11 & 14 & 17.
I'm not sure it's right but about your second error can you replace it with
std::this_thread::sleep_for(chrono::seconds(20000) );
to
std::this_thread::sleep_for(std::chrono::seconds(20000));
I think the first error depends from second, but I'm not sure.

C++ error: undefined reference to function

I have three files:
Two .cpp files (Reader.cpp and algo.cpp) and one header file (algo.h).
In Reader.cpp file, its function PacketHandler calls a function present in algo.cpp.
Reader.cpp
void PacketHandler(Packet* sniff_packet, void* user) {
std::string payload;
RawLayer* raw_payload = sniff_packet->GetLayer<RawLayer>();
if (raw_payload) {
/* Summarize some data */
cout << "[+] ------- [+]" << endl;
TCP* tcp_layer = sniff_packet->GetLayer<TCP>();
cout << "[#] TCP packet from source port: " << dec << tcp_layer->GetSrcPort() << endl;
cout << "[#] With Payload: " << endl;
payload = raw_payload->GetStringPayload();
SPPM(payload); // Function present in algo.cpp
}
}
void main() { // }
algo.h
#ifndef algo_H_
#define algo_H_
#include <string>
using namespace std;
void SPPM (std::string input);
#endif
algo.cpp
#include "algo.h"
#define byte uint8_t
using namespace std;
void SPPM(std::string input){ //definition }
When I compile the code using g++, I get this error:
In function `PacketHandler(Crafter::Packet*, void*)':
/home/maleeha/libcrafter_latest/libcrafter-master/libcrafter/pcap_reader.cpp:32: undefined reference to `SPPM(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'collect2: ld returned 1 exit status
I am executing my code this way:
g++ -c Reader.cpp -o Reader.o -g -lcrafter
g++ -c algo.cpp -o algo.o
g++ -o program Reader.o algo.o -lcrafter
./program Packets.pcap
Why is this error coming?

Qt compile error: endl will always evaluate to true

I have the following code snippet that compiles
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
for (QStringList::Iterator it = commandList.begin(); it != commandList.end(); ++it) {
out << "Command: " << *it << endl;
}
but always gives me this warning:
test.cpp:87: warning: the address of 'QTextStream& endl(QTextStream&)' will always evaluate as 'true' [-Waddress]
What does it mean and how do I fix it? Since a newline character is printing I assume this is not a namespace issue...
When you are using a binary data stream, it is not a good practice to start inserting new lines. That is one of the main points over a simple QTextStream.
This code works fine for me without any warning with gcc version 4.8.1 20130725 (prerelease) (GCC)
main.cpp
#include <QByteArray>
#include <QDataStream>
#include <QIODevice>
#include <QStringList>
int main()
{
QStringList commandList = QStringList() << "foo" << "bar" << "baz";
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
for (QStringList::Iterator it = commandList.begin(); it != commandList.end(); ++it)
out << "Command: " << *it;
}
Building
g++ -std=c++11 -Wpedantic -Wall -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core -fPIC main.cpp

main() first defined here

I'm trying to compile my project in Eclipse.
However, it says that the main() is defined more than once. I grep'd my project dir and it found only one definition of main(), in main.cpp.
Apparently it is somewhere else.maybe a dir I linked to.
The only dirs I linked to are:
-ljson_linux-gcc-4.5.2_libmt
The compiler output is:
make all
Building file: ../src/main.cpp
Invoking: GCC C++ Compiler
g++ -Ijson_linux-gcc-4.5.2_libmt -I/usr/include/mysql -I/usr/include/jsoncpp-src-0.5.0/include -O0 -g3 -Wall -c -fmessage-length=0 -Ijson_linux-gcc-4.5.2_libmt -MMD -MP -MF"src/main.d" -MT"src/main.d" -o"src/main.o" "../src/main.cpp"
Finished building: ../src/main.cpp
Building target: Atms
Invoking: GCC C++ Linker
g++ -L-L/usr/include/jsoncpp-src-0.5.0/include/ -o"Atms" ./src/atmstypes.o ./src/base64.o ./src/hregex.o ./src/libparser.o ./src/log.o ./src/main.o ./src/serv.o ./src/sqlfeeder.o ./src/teleindex.o ./src/telepipe.o ./src/telesharedobject.o ./src/treet.o ./src/ttable.o -l-ljson_linux-gcc-4.5.2_libmt
./src/serv.o: In function `main':
/usr/include/c++/4.4/new:101: multiple definition of `main'
./src/main.o:/home/idan/workspaceCpp/Atms/Debug/../src/main.cpp:12: first defined here
/usr/bin/ld: cannot find -l-ljson_linux-gcc-4.5.2_libmt
collect2: ld returned 1 exit status
make: *** [Atms] Error 1
main.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <iostream>
#include <string.h>
#include <string>
#include "../h/hregex.h"
using namespace std;
string s = "this and7 that";
int main(int argc,char** argv){
cout << hregex::InitRegex() << endl;
cout << hregex::CheckHostnameField(s)<< "= this and7 that" << endl;
s = "this and7 that";
cout << hregex::CheckURLField(s)<< "= this and7 that" << endl;
s = "/lol/idan.html";
cout << hregex::CheckURLField(s)<< "= /lol/idan.html" << endl;
s = "/lol2#/idan.html";
cout << hregex::CheckURLField(s)<< "= /lol2#/idan.html" << endl;
return 0;
}
How can I prevent the error from appearing?
g++ says serv.o has a main function.
If there actually is no main() it serv.cpp, check the includes, maybe you did a bad #include and included a .cpp instead of a .h ?
As an extra remark :
it tries to bind against the library "-ljson_linux-gcc-4.5.2_libmt"
So there is "-l-ljson_linux-gcc-4.5.2_libmt" in the link command line. Remove the -l in your configuration