Where does Homebrew install C++ packages? - c++

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

Related

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>

MinGW include paths conflict

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

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

Problems with std::stoi in C++ using ubuntu

I am receiving a compile error when I try to compile the simple program below.
error: ‘stoi’ was not declared in this scope
I've tried to include both #include <string> and #include <string.h> and I still am having those issues. I am using Ubuntu and I cannot remember how I installed g++ but I am sure it was using the apt-get install g++ command, so I do not know what version of g++ or the C++ libraries I am using.
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
struct Data
{
string fname;
string lname;
int age;
};
int main()
{
bool toContinue = true;
Data data;
string buffer;
do
{
try
{
getline(cin,data.fname);
getline(cin,data.lname);
getline(cin,buffer);
data.age = stoi(buffer);
cout<<data.fname<<" ";
cout<<data.lname<<" ";
cout<<data.age<<endl;
}
catch(std::invalid_argument)
{
cerr<<"Unable to parse integer";
}
}while(toContinue);
return 0;
}
My goal is to be able to use exception handling in case the user enters junk for any of the variables.
If you take a look at the documentation, you'll see that it was introduced in C++11. You'll have to compile your code with the -std=c++11 option to enable those features because code isn't compiled as C++11 by default.
Drew commented saying that if you are using C++03, you can use
boost::lexical_cast<int>(buffer)
As it turns out I needed this in order for it to work...
g++ -std=c++0x ./main.cpp

Boost precompiled headers problem

I try to precompile Boost headers.
First experiment - with std:: headers. I create file std.hpp:
#include <vector>
#include <iostream>
// And other std:: headers
After that:
g++ std.hpp
Copy std.hpp.gch in /usr/include/c++/4.4.5
And write test program:
#include <std.hpp>
int main() {
std::cout << "Hello, precompiled world!" << std::endl;
return 0;
}
Works fine.
Now try precompile Boost headers.
I create boost.hpp file:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
After that:
g++ boost.hpp
Copy boost.hpp.gch in /usr/local/include/boost
And write test program:
#include <boost/boost.hpp>
int main() {
// Some code...
return 0;
}
But got error:
main.cpp:2:33: error: /usr/local/include/boost/boost.hpp: No such file or directory.
Try, for experiment:
#include </usr/local/include/boost/boost.hpp>
int main() {
// Some code...
return 0;
}
Same error.
Try copy boost.hpp.gch in another place - same error.
If I put file boost.hpp in same place - works fine (so there is no problems with path):
ls /usr/local/include/boost | grep boost
boost.hpp
boost.hpp.gch
So compiler use boost.hpp header. But why compiler don't see precompiled boost.hpp.gch??
This might be a gcc bug as documented in http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46110