MinGW include paths conflict - c++

I'm trying to cross-compile on Linux for Win64 using MinGW-w64. Here's my code
#include <cstdlib>
#include <iostream>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
int main(int argc, char** argv)
{
if (argc > 1)
std::cout << std::atoi(argv[1]) << std::endl;
std::cout << boost::uuids::random_generator()() << std::endl;
return 0;
}
A simple compile fails with the error
$ x86_64-w64-mingw32-c++ hello.cpp
hello.cpp:4:31: fatal error: boost/uuid/uuid.hpp: No such file or directory
#include <boost/uuid/uuid.hpp>
^
Boost is installed in /usr/include which apparently the MinGW compiler doesn't search. If I add that path, then...
$ x86_64-w64-mingw32-c++ -I/usr/include hello.cpp
In file included from /usr/include/stdlib.h:314:0,
from /usr/x86_64-w64-mingw32/include/c++/4.9.1/cstdlib:72,
from hello.cpp:1:
/usr/include/sys/types.h:109:19: error: conflicting declaration ‘typedef __ssize_t ssize_t’
typedef __ssize_t ssize_t;
^
MinGW's cstdlib is including /usr/include/stdlib.h instead of /usr/x86_64-w64-mingw32/include/stdlib.h! How do I solve this? I need the -I in order to include Boost, but then MinGW includes other headers incorrectly.

You cannot use boost headers from /usr/include to cross-compile windows binaries.
You should also cross-compile boost.
See this guide for details about how to cross-compile boost on linux (it's for vle, but the first part is about boost):
http://www.vle-project.org/wiki/Cross_compilation_Win32
Update: Given that the guide is a little bit old, makes sense to link to boost documentation:
http://www.boost.org/boost-build2/doc/html/bbv2/tasks/crosscompile.html

Related

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

File too big compiling on Cygwin G++

I'm specifically building a test program to work on Chaiscript, which is how I encountered this issue:
chai.cpp:
#include <cstdio>
#include <iostream>
#include <chaiscript/chaiscript.hpp>
#include <chaiscript/chaiscript_stdlib.hpp>
std::string helloWorld(const std::string &t_name)
{
return "Hello " + t_name + "!";
}
int main(int argc, char** argv, char** env) {
chaiscript::ChaiScript chai;
chai.add(chaiscript::fun(&helloWorld), "helloWorld");
chai.eval("puts(helloWorld(\"Bob\"));");
return 0L;
}
/usr/lib/gcc/i686-pc-cygwin/5.4.0/../../../../i686-pc-cygwin/bin/as: CMakeFiles/chai.dir/src/chai.cpp.o: too many sections (37830)
/tmp/ccqGbeku.s: Assembler messages:
/tmp/ccqGbeku.s: Fatal error: can't write CMakeFiles/chai.dir/src/chai.cpp.o: File too big
/usr/lib/gcc/i686-pc-cygwin/5.4.0/../../../../i686-pc-cygwin/bin/as: CMakeFiles/chai.dir/src/chai.cpp.o: too many sections (37830)
This issue doesn't appear when I build on Mac or Linux.
I discovered a workaround to this issue from the Chaiscript CMakeLists.txt:
if(MINGW OR CYGWIN)
add_definitions(-O3)
endif()
Other searches on the Internet imply this big-object problem is linked the Windows executable format, and is not likely to be addressed in G++. Using MingW32 did not address this error in my case - I'm not going to 64-bit.
Object file has too many sections

compile c++ boost test program on command line

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>

How to link boost thread with Xcode

I have built Boost from the website using
./bootstrap.sh
./b2 install
I think all are installed properly. I have headers in /usr/local/include/boost and libs in /usr/local/lib.
Everything links as long as don't include boost/thread.hpp:
//
// main.cpp
// ising3
//
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <iostream>
#include <boost/date_time.hpp>
//#include <boost/thread.hpp>
using namespace ::boost::tuples;
using namespace ::boost;
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
tuple<int,int> a,b,c;
a=make_tuple(1,1);
b=make_tuple(3,2);
std::cout<<a;
return 0;
}
Runs and prints:
Hello, World!
(1 1)
However, if uncommented, it fails:
I have linked the dynamic library libboost_thread.a and libboost_thread.dylib and included /usr/local/include into header search path and /usr/local/lib into library search path.
You should also link to boost_system.
The error indicates that it needs boost::system::system_category (which exists for error reporting).

GCC iostream fstream error in Ubuntu 13.10

I am using Ubuntu 13.10. I am getting some errors for the following code.
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>
int main(int argc, char *argv[])
{
error.set_program_name(argv[0]);
if ( argc != 2 )
{
// printf(argv[0] + " usage: fifo_client [string] \n");
/// cout << argv[0] << " usage: fifo_client [string]" << endl;
exit(EXIT_FAILURE);
}
ofstream out(fifo_file);
if(out)
out << argv[1] << endl;
return(EXIT_SUCCESS);
}
If I run the above program a.c using command
gcc a.c -o a
a.c:1:20: fatal error: iostream: No such file or directory
#include <iostream>
^
compilation terminated.
I don't know whats the problem.
Use g++ instead of gcc. gcc could compile a c++ file if it had the right extension (.cpp for instance) or with the right arguments (-x c++) but adding the arguments needed to link with the C++ libraries is far too complex to avoid the simple solution.
The problem is that you're mixing C & C++ code and compiling it using GCC.
try
#include <fstream>
using namespace std;
instead of #include <fstream.h>
anyway your source code is not full to make correct suggestion.
I ran your code in my compiler and got following error :-
test2.c:3:21: fatal error: fstream.h: No such file or directory
#include <fstream.h>
^
compilation terminated.
so i think your question has typo.
It is because you are mixing c and c++ code, fstream is part of c++. try to run by g++.