i want to use MongoDB from my C++ application, I've downloaded the MongoDb binary Version 3.0.5 and the legacy C++ Driver Version 1.0.5 from Git and installed both.
I followed the instruction with this code:
#include <cstdio>
#include <mongo/bson/bson.h>
#include <mongo/client/dbclient.h>
int main(int argc, char *argv[])
{
mongo::Status status = mongo::client::initialize();
return 0;
}
After Compile and Run i get to following message:
Attempt to add global initialiser failed, status: DuplicateKey GlobalLogManager Abort
Some ideas ?
I can reproduce the same behavior when using legacy driver compiled for C++03 (default) and application code compiled with C++11/C++14. It always segfaults and sometimes writes the same message (depending optimization level). See the related bug on mongo website.
The solution is to either:
compile your code with C++03 (-std=c++03)
recompile the driver with C++11 support.
For the C++11 support in driver, simply pass --c++11=on to scons.
scons --c++11=on install
Tested with GCC 4.9.1
Related
If I create a simple library (built using CMake):
foo.h
#pragma once
#include <functional>
void foo(std::function<void()> handler);
foo.cpp
#include "foo.h"
void foo(std::function<void()> handler)
{
handler();
}
And then create a separate CMake project compiling an executable:
#include "foo.h"
#include <cstdlib>
#include <iostream>
int main()
{
foo([&]()
{
std::cout << "hello" << std::endl;
});
return EXIT_SUCCESS;
}
And then compile the library using MSVC (using latest platform SDK 10.0.17134.12) in Release or RelWithDebInfo (remember I'm using CMake) and I compile the executable using Clang 6 (Clang 5 is not supported with the latest Windows Platform SDK bundled with Visual Studio 17.7 https://blogs.msdn.microsoft.com/vcblog/2018/05/07/announcing-msvc-conforms-to-the-c-standard/) also using Release or RelWithDebInfo, then I get heap corruption triggered by the std::function destructor.
To trigger heap corruption reliably, I can use gflags.
gflags -p /enable "myexe.exe" /full
It worked fine with Clang 5 on the previous platform SDK (standard Clang available for download from LLVM, not Microsoft's version). I'm assuming this is an ABI (or similar) problem or bug. It's taken a while to distill this all down and get a minimum verifiable reproduction. Does anyone have any suggestions about where to take it from here?
It does appear to be an ABI issue from what I can tell, so I reported a bug:
https://bugs.llvm.org/show_bug.cgi?id=39962
Update:
The bug was fixed in https://reviews.llvm.org/rL328386 and is included in Clang 7.0.0.
My laptop can not compile a simple c++ code since yesterday, it works perfectly fine before.
The c++ code is can be a hello-world code in main.cpp file.
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
cout<<"Hello World"<<endl;
return 0;
}
I am trying to compile the code by
icpc main.cpp
The error information is
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm(637),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/__string(56),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/string_view(171),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/string(470),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/__locale(15),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/ios(216),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/iostream(38),
from main.cpp(1):
/Library/Developer/CommandLineTools/usr/include/c++/v1/type_traits(2065): error: expected an identifier
: public decltype((_VSTD::__is_assignable_test<_Tp, _Arg>(0))) {};
compilation aborted for main.cpp (code 2)
A few information:
I am using icpc (ICC) 17.0.4 20170411, it is installed from IntelĀ® Parallel Studio XE Composer Edition for C++ macOS.
My mac is MacBook Pro (15-inch, 2017), version 10.12.6.
If I use gnu compiler, it works fine. While my code needs to use intel's compiler.
The code works before, do not know while it becomes this. I have already tried restarting the systems.
======================================================================
Update1: The problem happened after I update my "Command Line Tools for Xcode". It looks like the /Library/Developer/CommandLineTools/usr/include/c++/ is not right.
======================================================================
Update2: This is can be solved by using icpc -std=c++11 main.cpp
However when I change my main.cpp to
#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
tuple<vector<int>, vector<int>, vector<int>>
getAllBlockMeanErrorTuple(const vector<int> &vec)
{
vector<int> fact, mean, err;
fact.resize( vec.size() );
mean.resize( vec.size() );
err.resize( vec.size() );
return make_tuple(fact, mean, err);
}
int main(int argc, char** argv)
{
cout<<"Hello World"<<endl;
return 0;
}
It has error again even if I use icpc -std=c++11 main.cpp
/Library/Developer/CommandLineTools/usr/include/c++/v1/__tuple(401): error: type name is not allowed
-> __all<typename enable_if<_Trait<_LArgs, _RArgs>::value, bool>::type{true}...>;
detected during:
I encountered the same issue while upgrading command line tools to the version of September 2017
While not finding a proper solution, I reinstalled previous version ( April 2017) of command line tools and it solved the problem (https://developer.apple.com/download/more/#).
I am looking forward to having a clean solution.
EDIT (5/12/17):
I solved the issue by recompiling everything using gcc. At compilation, Intel compilers will use the compiler that responds to gcc and g++ in the path. An installation with homebrew and some symlink in /usr/local/bin pushes the newly installed gcc in front of clang and then avoids gcc to change at each system update. Hope it helps.
Try to check that you are using right settings
and GNU is working because it automatically set to C++
try to set compiler to c++
hope this works.
OR You can use xcode to write c++ Code.
I'm experiencing .Call issues when running functions built with Rcpp on Windows, if my c++ code uses C++11 std::regex and I have found no way out so far.
Unlike prior questions on similar issues, I have had neither building nor linking issues. The Rcpp package builds and links fine using the C++11 plugin, making usable packages on my platform. constexpr and C++11-specific functions like std::stoi cause no issue when std::regex is not used.
Using Windows boost libs, I experienced linking issues, even when specifying PKG_LIBS="-L/path/to/boost/libs -lboost_regex", so I'd rather stick to std::regex.
The same packages build, install and run fine under linux, using vanilla std::regex or boost::regex.
I unfortunately found no solution in the fine Rcpp gallery examples.
Windows platform is :
R version 3.2.3 (2015-12-10)
x86_64-w64-mingw32/x64 (64-bit)
Running under:
Windows >= 8 x64 (build 9200)
Rcpp_0.12.3
Rtools 3.3.0.1959 running g++ 4.9.3 (x86_64-posix-seh,
built by MinGW-W64 project), normally C++11-compatible.
PKG_CXXFLAGS="-std=c++11"
The linux platform is similar except for g++ (version 5.3).
Below is a simplified code chunk for duplication.
#include <Rcpp.h>
#if defined(__linux__) && ! defined(FORCE_STL_BUILD)
#include <boost/regex.hpp>
#define reglib boost
#else
#include <regex>
#define reglib std
#endif
#include <string>
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
constexpr int a[3]= {2, 10, 15};
// [[Rcpp::export]]
int my_test(int prop, const std::string& index)
{
#ifndef NO_REG
static const reglib::regex test {"H.*A", reglib::regex::icase};
#endif
int index_int = std::stoi(index) + a[1] + prop;
return index_int;
}
This code runs OK when built using -DNO_REG. Otherwise invoking test::my_test(1, "1000") returns:
`Error in .Call("test_my_test", PACKAGE = "test", prop, index) :
"test_my_test" not available for .Call() for package "test"`
EDIT:
1. The question focuses on std::regex. Boost issues are only incidental comments.
2. Issues only arise after packaging, not using Rcpp::source("cppfile")
3. Packaging code:
R console:
Rcpp::Rcpp.package.skeleton("test", attributes=TRUE, example_code=FALSE, cpp_files="test.cpp")
Rcpp::compileAttributes("test")
CMD console:
REM paths to R/bin/x64 and Rtools/bin, Rtools/mingw_64/bin added to PATH
set PKG_CXXFLAGS=-std=c++11
R CMD build test
R CMD INSTALL test_1.0.tar.gz
ADDITIONAL EDIT:
.Call issues arise as soon as a regex is declared in the C++ code. Using it or no (as in std::regex_match) makes no change.
Can you try disentangling this some more? You are mixing a lot of things here.
Try maybe 'just' C++ from R first, with the newer g++ 4.9.3 compiler and see if that lets you use Boost as you hope. Your use case there is local and non-standard, so you have to work this out. We generally just recommend using BH without linking.
I don't actually see an Rcpp issue here. You are simply pushing the (working, tested, trusted) Rcpp setup into a corner it has not been used in yet. So you may need to work some things out yourself.
Also note that g++ 4.9.3 for R is not really released yet.
I want to include the C++ Libary called URDL. I am developing a program on Xcode with Clang Compiler using Boost 1.56.
As a Sample Project I used this code here and include the Boost Libaries- and Include-Paths in the settings of my Xcode Project.
#include <iostream>
#define URDL_HEADER_ONLY 1
#include <boost/array.hpp>
#include <urdl/http.hpp>
#include <urdl/istream.hpp>
int main(int argc, const char * argv[])
{
urdl::istream is;
return 0;
}
Then I compile I get the following Errors:
in http.ipp from URDL
/Users/maximilian/XcodeProjects/Libaries/urdl-0.1/include/urdl/impl/http.ipp:25:23: Exception specification of overriding function is more lax than base version
and in handler_invole_helpers.hpp from URDL
/usr/local/Cellar/boost/1.56.0/include/boost/asio/detail/handler_invoke_helpers.hpp:37:3: Call to 'asio_handler_invoke' is ambiguous
My suggestion is that URDL is not compatible with this Version of Boost (1.56).
Thanks in Advance
lux_
PS: I must use URDL or at least boost::asio (which URDL is based on, but URDL makes my work much simpler) because it is a homework for an university course.
Problem Solved ! :) Download the URDL Version from Github instead of the Website think-async.com as I did before.
First of all I'm quit new in programming in c/c++, so maybe it's just a basic error but i don't get it. The Problem is, I want to use libpcap in a bigger application, but i can't even integrate the libary into a simple HelloWorld.cpp. Atm I'm using Eclipse (MinGw Compiler) with Windows 64bit, but consindering to switch to Linux if my Problem(s) can't be solved.
Downloaded "libpcap-1.5.3.tar.gz" from tcpdump.org, extracted and added the root directory of the Libary to Eclipse (under "Properties/C|C++ Build" to all Compilers and to the MinGW Linker).
My Code:
#include <iostream>
#include <pcap.h>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[]) {
cout << "Hello World!!!" << endl; // prints Hello World!!!
char *dev = argv[1];
printf("Device: %s\n", dev);
return(0);
}
First error:
D:\Tools\MinGW\lib\libpcap/pcap-stdinc.h:49:22: fatal error: bittypes.h: No such file or directory
So I changed line 49 from
#include "bittypes.h"
to
#include <Win32/Include/bittypes.h>
(the actual location of bittypes.h) and next error pops up:
D:\Tools\MinGW\lib\libpcap/pcap/pcap.h:451:1: error: 'Adapter' does not name a type
'Adapter' is defined in "pcap-int.h" so I included this header into "pcap.h" but now I get:
D:\Tools\MinGW\lib\libpcap/pcap-int.h:46:22: fatal error: Packet32.h: No such file or directory compilation terminated.
and this "Packet32.h" does not exist...
I can't belive this popular libary is broken, so where is my mistake? Thanks in advance!
The Problem is, I want to use libpcap in a bigger application, but i can't even integrate the libary into a simple HelloWorld.cpp. Atm I'm using Eclipse (MinGw Compiler) with Windows
If you want to use libpcap on Windows, you need a version of libpcap that's been ported to Windows. The libpcap source won't build on Windows; on UNIXes, libpcap can and does use packet capture mechanisms built into the OS, but it doesn't do so on Windows (for one thing, older versions of Windows, at least, don't have a packet capture mechanism built in!), so it also needs a driver.
One port of libpcap to Windows is WinPcap; it includes the necessary driver. If you install WinPcap, and then download the WinPcap developer's pack and install it, it should be possible to configure Eclipse so that your program can be built with WinPcap.
libpcap-1.5.3.tar.gz is a source package for the library, not a binary package. So you need to build the library; you can't just add the directory to Eclipse and expect it to work.
The usual way to build a source package is to unpack it into a directory and look for a file called README or INSTALL, which is a simple text file containing instructions on how to build and install the package for various machines. Often there will be multiple such files for different platforms.
Generally, there will be a script called configure that you run to create a Makefile for your target; then you run make to build the code and make install to install it in a standard place so that other packages can find it.