how to include cxxopts library in compiler - c++

I'm trying to pass the arguments from command line using command line parser library, in C in we can use the getopt() function but I am writing in C++ so I have to use cxxopts parser library, which on execution gives a fatal error that there is no such kind of library, I want a way to use cxxopts
i tried using cxxopts as #include<cxxopts.hpp> along with #include<iostream>
#include<iostream>
#include<cxxopts.hpp>
main(int argc, char *argv[])
{
int i;
for(i=0;i<argc;i++)
cout<<argv[i];
}
prob.cpp:2:22: fatal error: cxxopts.hpp: No such file or directory
#include
^
compilation terminated

I am using Arch Linux. According to the INSTALL, I download the newest release from here, move the cxxopts.hpp from the include to /usr/include/, then compile the example.cpp with clang++ example.cpp and run it:
$ ./a.out -a
Saw option ‘a’ 1 times
Arguments remain = 1
Saw 1 arguments
It works.
In the example.hpp, it has:#include "cxxopts.hpp"

Related

can not build cpp using clang pre-built binary: file wchar.h not found

I am using MacOS 10.15. Since the clang shipped with MacOS does not include clang-format. I installed another pre-built clang binary from here. I have added the binary file path to my PATH variable.
export PATH="$HOME/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin:$PATH"
I tried to compile a simple program:
#include <iostream>
int main(int argc, char *argv[]) {
std::cout << "Hello world!\n";
return 0;
}
using the following command:
clang++ hello.cpp -o hello
I got the following error:
In file included from hello.cpp:1:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/iostream:37:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/ios:214:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/iosfwd:95:
/Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/wchar.h:118:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
^~~~~~~~~
1 error generated.
I found that wchar.h bundled with this pre-built package is in the following directory:
/Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/include/c++/v1/
So I added the -I flag:
clang++ -I /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/include/c++/v1 hello.cpp -o hello
The error still persists.
If I use clang++ shipped with MacOS, I have no problem compiling the source code:
# the following works without any error
/usr/bin/clang++ hello.cpp -o hello
I have seen post here, here, and here, but the solutions do not apply.
You got clang-format improperly. Reset the system to the state before you installed another pre-built clang binary. Then use Homebrew to install clang-format
brew install clang-format
clang+llvm-10.0.0-x86_64-apple-darwin is not suitable to your Mac. It depends on system frameworks that are not available, so you get the error finding wchar.h in a system framework. When you install clang+llvm-10.0.0-x86_64-apple-darwin you ignore framework dependencies. Homebrew will care about dependencies.

C++ - Undefined reference to "sodium_init"

I am attempting to make a testing application using libsodium, however I am getting the error:
main.cpp:6: undefined reference to `sodium_init'
I ran the following commands to install in as the root user.
$ ./configure
$ make && make check
$ make install
This the code that is having the issue.
#include <stdio.h>
#include <sodium.h>
int main(int argc, char **argv)
{
if (sodium_init() == -1)
{
return 1;
}
printf("libsodium had no issues!\n");
return 0;
}
I am using CodeLite as my IDE, and my C++ compiler options are the following:
-g;-O0;-Wall;-lsodium
The options were default and I added -lsodium to the list.
Attempting to compile main.cpp directly from the terminal with the following command g++ -lsodium main.cpp throws the same error.
Could someone please help me with my issue.
Libraries for linking are searched in order, so you need to place the libraries after your local translation units:
g++ main.cpp -lsodium
In your IDE, make sure you add -lsodium as a linker argument.

Cannot include standard libraries in C++ file

#include <iostream>
using namespace std;
int main(){
std::cout << "Hello World\n";
return 0;
}
command 1 (works)
clang hello.cc -o hello -lc++
command 2 (don't works)
/path/to/custom/clang hello.cc -o hello -lc++
main.cc:2:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Why I can't compile with command 2 ?
It looks like you're trying to compile C++ with a C compiler. Try running clang++ instead.
clang++ hello.cc -o hello
Without running clang as a C++ compiler it won't have the C++ standard library headers available for you to include. Using clang++ the C++ standard library headers are available and the C++ standard library is linked for you automatically.
That is a known Ubuntu issue. Their clang just isn't set up right. I complained about it here -- and this remained unfixed for years.
But the good news is that it now works with the most recent 16.10 release.
Edit: Based on your updated question I would say that "custom clang" does not know about its include files.

g++ #include file not found compiler error

I'm currently trying to simply link a file called main.cpp with boost in order to do asio(asynchronous input and output). I'm using a makefile in order to compile the C++ code into executable form. However, despite using the -I modifier on my terminal command in Mac OS X, it still cannot find the appropriate directory to search in. Here is my code in Main
#include <asio.hpp>
int main(){
return 1;
}
and here is the makefile command that I am using
all:
g++ -Iboost_1_60_0/boost/ -o main main.cpp
In my file structure, boost_1_60_0, main.cpp, and makefile are all on the top level, where asio is in the folder boost which is in the folder boost_1_60_0. I'm very confused about this and any help would be appreciated! Thanks!
EDIT: Full error is
1 error generated.
make: *** [all] Error 1
iMats-2:SerialC++ wfehrnstrom$ make
g++ -Iboost_1_60_0 -o main main.cpp
main.cpp:2:10: fatal error: 'asio.hpp' file not found
You should use -Iboost_1_60_0 (or, better still, install Boost properly so that it's found automatically under /usr/include/), and write #include <boost/asio.hpp>.
The include directives inside Boost itself will assume this form, so…
BTW, this has nothing to do with exceptions. Presumably it's a compiler error you've seen.

Clang fails to find stdio.h. Which flag should I add?

Here is a simple program
#include <stdio.h>
int main(){
printf("Hello World\n");
return 0;
}
Compiling this program with Clang gives the error below.
$ clang test.cpp
test.cpp:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^
1 error generated.
The program above is a shortened version from a large project. I can neither change the program, or change the compiler to use, namely clang. So it seems to me the only options is to change environment variables so to alter the flags used by Clang. Which flag should I use?