Linking g++ compiled code against libraries created by clang++ - c++

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++?

Related

“Program received signal SIGABRT, Aborted.“error in remote Linux,why? [duplicate]

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

Where does Homebrew install C++ packages?

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

can't run a c++ file - Eclipse Yoxos

I downloaded Eclipse from Yoxos. This Eclispe includes: c, cpp, java etc..
However, when I opened a new cpp project with MinGW GCC Toolchains, and created a cpp file: hello.cpp, and wrote the following little program:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
when I run the file, it said - hello.exe has stopped working.
However, When I changed the program to:
(first line in comment)
//#include <iostream>
#include <stdio.h>
int main() {
printf("dsd");
return 0;
}
It worked well!
and when I removed the first line from comment like this:
#include <iostream>
#include <stdio.h>
int main() {
printf("dsd");
return 0;
}
the problem was back.. :(
Someone, Help..?
Thanks in advance! :)
Build Console Output:
20:41:10 **** Incremental Build of configuration Release for project hello ****
Info: Internal Builder is used for build
g++ -O3 -Wall -c -fmessage-length=0 -o helo.o "..\\helo.cpp"
g++ -o hello.exe helo.o
20:41:11 Build Finished (took 610ms)

Thread doesn't work with an error: Enable multithreading to use std::thread: Operation not permitted

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

Code with XCppRefl does not work

I am trying to use XCppRefl lib to achieve reflections in c++. http://www.extreme.indiana.edu/reflcpp/. I could successfully install this library in linux and run the tests given with the source code of the library.
Here is the code that I have written --
#include <iostream>
using namespace std;
#include <reflcpp/ClassType_tmpl.hpp>
#include <reflcpp/BoundClassType_tmpl.hpp>
#include <reflcpp/Exceptions.hpp>
using namespace reflcpp;
#include "Complex.h"
int main()
{
//ClassType ct = ClassType::getClass( string("Complex") );
////PtrHolder_smptr_t obj = ct.createInstance();
//assert(ct.name() == "B");
Complex x;
int ret;
Complex a;
ClassType c = ClassType::getClass( string("Complex") );
//cout<<"name :: "<<c.name()<<endl;
}
It seems to compile just fine --
$ g++ -g -I /usr/local/include/reflcpp-0.2/ -L /usr/local/include/reflcpp-0.2/ -lreflcpp main.cpp
However when I execute the executable (a.out), I get a core-dump
a.out: Type.cpp:87: static const reflcpp::Type_body* reflcpp::Type_body::getType(const std::string&): Assertion `s_class_name_map' failed.
Aborted (core dumped)
Does anyone has used this lib before? Please help.
you have to link your main.o to libreflcpp.a . after compiling use this:
g++ -p -pg -o"project_name" ./A.o ./A_reflection.o ./main.o /usr/local/lib/libreflcpp.a