Hello, I want to compile some demo of mongodb-C++ API.
I follow the official guide:
http://mongocxx.org/mongocxx-v3/installation/
It is OK for step 1-5.
For use custom bson and mongocxx, I run the additional command as recommend:
In the event that you are building the BSON C++ library and/or the C++
driver to embed with other components and you wish to avoid the
potential for collision with components installed from a standard
build or from a distribution package manager, you can make use of the
BSONCXX_OUTPUT_BASENAME and MONGOCXX_OUTPUT_BASENAME options to cmake.
cmake .. \
-DBSONCXX_OUTPUT_BASENAME=custom_bsoncxx \
-DMONGOCXX_OUTPUT_BASENAME=custom_mongocxx
The mongo-db demo is used:
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
bsoncxx::builder::stream::document document{};
auto collection = conn["testdb"]["testcollection"];
document << "hello" << "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
When I run with the command:
c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)
It feedbacks some error:
#include <bsoncxx/builder/stream/document.hpp
It looks like the header file has not been included.
How can I resolve the problem?
Thanks & Regards!
Related
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 am looking for a full sample that covers usage of the LLVM C++ API, particularly loading a function from a bitcode (not a typo, they call it bitcode) file, running it and get the results. I have studied this blog post and I am trying to port it to C++ but I am struggling to understand how to create the various instances needed, particularly the execution engine. I am using clang -c -emit-llvm file.c to compile a C file to a .bc LLVM bitcode file. The command clang -S -emit-llvm file.c also works and generates a textual .ll file. The function parseIRFile seems to be able to load both.
This is what I have so far:
LLVMContext context;
SMDiagnostic error;
unique_ptr<Module> mod = parseIRFile(StringRef(pathToLlOrBcFile), error, context);
I does not have to use JIT, I am fine with the basic interpreter for now; but I wish to make it work with MCJIT or whatever it's called later on.
Thanks to #arnt for noticing that I was actually using the IR text format; I changed the Makefile and the C++ app to reflect the fact that both .ll and .bc can be parsed by the same function.
I am using llvm-devel.x86_64 9.0.1-5.fc31 on Fedora 31. Full code below.
main.cc (This is the C++ app that loads the LLVM bitcode)
#include <iostream>
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/GenericValue.h>
using std::unique_ptr;
using std::cout;
using std::endl;
using llvm::Module;
using llvm::SMDiagnostic;
using llvm::LLVMContext;
using llvm::parseIRFile;
using llvm::StringRef;
using llvm::ExecutionEngine;
using llvm::EngineBuilder;
using llvm::ArrayRef;
using llvm::GenericValue;
using llvm::Function;
int main(int argc, char const *argv[]) {
LLVMContext context;
SMDiagnostic error;
unique_ptr<Module> mod = parseIRFile(StringRef("hosted.bc" /* .ll files also work */), error, context);
ExecutionEngine *executionEngine = EngineBuilder(std::move(mod)).setEngineKind(llvm::EngineKind::Interpreter).create();
Function *add = executionEngine->FindFunctionNamed(StringRef("add"));
GenericValue param1, param2;
param1.FloatVal = 5.5;
param2.FloatVal = 2.7;
GenericValue params[] = { param1, param2 };
ArrayRef<GenericValue> args = ArrayRef<GenericValue>(params, 2);
GenericValue result = executionEngine->runFunction(add, args);
cout << param1.FloatVal << " + " << param2.FloatVal << " = " << result.FloatVal << endl;
}
hosted.c (This is a C app that I compile into an .bc file with clang)
float add(float a, float b) {
return a + b;
}
Makefile (Used to compile the native app and the LLVM bytecode to be hosted in it)
app.o: main.cc
g++ main.cc -lLLVM -o app.o
hosted.bc: hosted.c
clang -c -emit-llvm hosted.c
clean:
rm app.o
rm hosted.bc
.PHONY: clean
Output (Compiling and running)
[dario#localhost llvm-cpp-first]$ make hosted.bc && make && ./app.o
clang -c -emit-llvm hosted.c
g++ main.cc -lLLVM -o app.o
5.5 + 2.7 = 8.2
In my Homebrew installation my libraries are compiled with clang, whereas I would like to, for performance reasons, compile my scientific code with gcc. In order to understand this problem better, I have created a minimal test:
// FILE print.cxx
#include <string>
#include <iostream>
void print_message(const std::string& message)
{
std::cout << message << std::endl;
}
// FILE test.cxx
#include <string>
void print_message(const std::string&);
int main()
{
std::string message = "Hello World!";
print_message(message);
return 0;
}
This code I compile with:
// SCRIPT compile.sh
clang++ -stdlib=libstdc++ -c print.cxx
g++ test.cxx print.o
The example that I have added works, but is it possible to make it work with libraries that are compiled without the -stdlib=libstdc++ flag and instead use the libc++?
I've registered an account at exercism.io and is working on the c++ test case. Trying to wrap my head around boost test I created this simple bob.cpp program:
#include "bob.h"
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[]) {
string s = bob::hey("Claus");
cout << s << endl;
return 0;
}
bob.h:
#include <string>
namespace bob {
std::string hey(std::string s) {
return "Hello " + s;
}
}
Compiling in terminal with 'clang++ bob.cpp' and running with ./a.out works. Wrote a boost test using this link: c++ Using boost test
bob_test.cpp:
#include "bob.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(greeting) {
BOOST_CHECK_EQUAL("Hello Claus", bob::hey("Claus"));
}
But when I try to compile it using
~/devel/cpp/boost%>clang++ -I /opt/local/include -l boost_unit_test_framework bob_test.cpp
ld: library not found for -lboost_unit_test_framework
clang: error: linker command failed with exit code 1 (use -v to see invocation)
regards
Claus
This is on Yosemite with Xcode 6.0.1, boost 1.56 installed via macports. Tried on a Mavericks with same Xcode and boost 1.55 but same result.
I got it working by changing the parameters passed to the linker:
clang++ -I /opt/local/include -Wl,/opt/local/lib/libboost_unit_test_framework.a bob_test.cpp
^^^^
and provide the complete path to the library.
And to enable c++11 features add this:
-std=c++11
You forgot the library path:
$ clang++ -I /opt/local/include -L /opt/local/lib -l boost_unit_test_framework bob_test.cpp
^^^^^^^^^^^^^^^^^
The link error you get after fixing this indicates that you have no main() function - it seems that the boost unit test framework will generate this for you provided you have all the necessary boilerplate in place - see http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/utf/user-guide/test-organization/auto-test-suite.html for details, but it looks like you may need:
#define BOOST_AUTO_TEST_MAIN
#include <boost/test/auto_unit_test.hpp>
rather than:
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
I created and executed a simple thread on my system. when I execute this program, I have the error message : Enable multithreading to use std::thread: Operation not permitted
some details about my system :
linux ubuntu 13.10
g++ 4.8.1
I compile the source code including the library pthread
The source code:
#include <iostream>
#include <thread>
using namespace std;
void func(void) {
cout << "test thread" << endl;
}
int main() {
cout << "start" << endl;
thread t1 (func);
t1.join();
cout << "end" << endl;
return 0;
}
It seems that you are trying to use C++11 threads. If it is true, then
correct #include <thread> and #include <iostream>, i.e. do not use " in these lines and add # in front of them.
compile with g++ -std=c++11 q.cpp -lpthread (dependency order matters for newer g++)
Hint: when you are using threads in a static linked library and use this library in an executable, then you have to add the flag -pthread to the link command for the executable. Example:
g++ Obj1.o Obj2.o MyStaticLib.a -o MyExecutable -pthread