Why cannot I compile my code with g+ in my terminal [duplicate] - c++

I just started using MinGW for Windows. When trying to create executable using
g++ a.cpp -o a.exe -std=c++14
for the code below:
#include <string>
using namespace std;
int main()
{
string x = to_string(123);
return 0;
}
I'm getting following error:
C:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../libmingwex.a(vsnprintf.o):(.text+0x0): multiple definition of vsnprintf
C:\Users\..\Local\Temp\cc4sJDvK.o:c:/mingw/include/stdio.h:426: first defined here
collect2.exe: error: ld returned 1 exit status
What is the root cause for this error and how can I make it go away? While I can easily find a replacement for to_string() function I'm not sure what is causing this error to occur in the first place.

Installing MinGW packages mingw32-libmingwex-* will link an appropriate version of vsnprintf and avoid the linker error.

This issue, i.e. multiple definition of vsnprintf, still exists in MinGW as December 2019.
After investigating a lot, I found the solution in the official mailing list.
It's a bug in mingwrt-5.2.2. Downgrading to the mingwrt-5.2.1 version solves that issue.
To do that, just input the following command:
mingw-get upgrade mingwrt=5.2.1
Then restart the MinGW shell.
Read the full story here.
Note: MinGW-w64 and MinGW are separate projects, so the accepted solution is not so helpful to me, as I want to keep MinGW and not to move to MinGW-w64.

I solved this issue using MinGW w64 compiler
download mingw-w64-install.exe
setup to Version: 6.3.0, Architecture: i686, Threads: posix, Exception: dwarf and Build revision: 2.
I hope this will be of some help.

There are multiple definitions of vsnprintf in both stdio.h and libmingwex.a. I am able to work this around by adding #define __USE_MINGW_ANSI_STDIO 0 to the start of the code, before any includes, which disables the definition of vsnprintf in stdio.h.

Related

How do I install libraries for <stropts.h>?

I am running Kali-Linux (debian+gnome). When I compile I get compilation error:
cc -Wall -g -c -o frag.o frag.c
frag.c:7:10: fatal error: stropts.h: No such file or directory
7 | #include <stropts.h>
| ^~~~~~~~~~~
compilation terminated.
make: *** [<builtin>: frag.o] Error 1
Can anyone tell me what am I missing and what should I install?
I tried installing glibc-sources but still it didn't work out.
As mentioned by the other answer, this library is not used on Linux. Since this came up when trying to compile an application on Linux, it's possible an #if was not set correctly.
As a workaround, look at the source code to see what the #if surrounding the #include is, and set that to false when compiling.
For example, if the code looked like:
#if HAVE_STROPTS_H
#include <stropts.h>
#endif
And if you are using cmake or gcc, run them with -DHAVE_STROPTS_H=0.
stropts.h is part of the Posix STREAMS extension, which Linux never supported. (Since 2008, it has also been marked as obsolescent by Posix, so it may be removed in some future standard revision.)
There have been third-party implementations of STREAMS for Linux, but I don't kniw if any of them are still supported. One which is used by Linux-based telephony apps is contained in openss7.
Another reason not yet mentioned might be the confusion manual introduces. If you need to use ioctl() call, the quickest way to find out the header to include might be pulling up a man ioctl. Well, it turns out there are 2 different manual pages for ioctl(), and having both on the system will result in this misleading and wrong suggestion (accessible directly as man 3 ioctl) of including the stropts.h.
More likely you actually need a sys/ioctl.h, described in man 2 ioctl:
#include <sys/ioctl.h>

How to solve comiler error: 'v_bias' is not a namespace-name

Sort briefing what I am trying to do:
I want to use vnode-lp on my windows PC. I’ve installed MinGW. I’ve installed the necessary dependencies like LAPACK & BLAS libraries as well as Profil/BIAS. The installation of all libraries passed the make and make install process without errors. I hope (!) I’ve managed to install it correctly.
Now the Problem:
Now I’ve tried to get a simple program compiled with basically northing in it just an #include “vnode.h”. First I tried it with Microsoft Visual Studio. Since this gave me several errors I tried to compile it with g++ using MinGW. This gives me the same errors. It starts with
./matrix.w:90:17: error: ‘v_bias’ is not a namespace-name
The Question:
How to include vnodelp into an c++ program and compile it without errors under windows, am I missing something?
Off-topic:
I am trying to get this running for over a week now and don’t know what to do anymore.
C++ is most definitely not C, and packages designed for C++ will never compile and run as C code. So, what you're trying to do really can't be done unless you do some fancy stuff by creating a .dll or something like that, and even then I think you wouldn't get the functionality you want. Why not write your code in C++ and compile with g++ or a similar compiler?
I have managed to finally solve this issue. In case someone has the same problem here is the solution.
I have missed something in the call. Here is the full call that has worked for me:
g++ -o2 –Wall –Wno-deprecated –DNDEBUG – DPROFIL_VNODE – DMAXORDER=50 –I(path to profil bias)/include –I(path to profil bias)/include/BIAS –I(path to profil bias)/src/Base –I(path to vnodelp)/FADBAD++ -I../include –DNDEBUG –c –o (filename).o (filename).cc
g++ -L(path to profil bias)/lib –L(path to lapack)/lib –L../lib –o (filename) (filename).o –lvnode –lProfil –lBias –llr (path to lapack)/lib/liblapack.lib (path to lapack)/lib/libblas.lib –lstd++
funfact:
This also complies with gcc instead of g++

MySQL Connector/C++ in Xcode 9.2 via Homebrew usage problems

I've successfully installed MySQL, Boost and mysql-connector-c++ on my macOS High Sierra 10.13.3 via Homebrew, but i've ran into problems with using those libs in Xcode.
So, my boost and connector libs are located at /usr/local/Cellar/. So i just wrote a simple code sample to check if everything "works":
#include <mysql_driver.h>
#include <mysql_error.h>
#include <mysql_connection.h>
int main()
{
return 0;
}
and compiled it with
c++ -I /usr/local/Cellar/mysql-connector-
c++/1.1.9_1/include/mysql_connection.h /usr/local/Cellar/mysql-connector-
c++/1.1.9_1/include/mysql_driver.h /usr/local/Cellar/mysql-connector-
c++/1.1.9_1/include/mysql_driver.h main.cpp
Everything seems to be working, except I get some warnings:
clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode,
this behavior is deprecated [-Wdeprecated]
clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode,
this behavior is deprecated [-Wdeprecated]
But that's not a huge issue, I think.
And then I try to use those libs in Xcode, so i do this:
Yet when I try to compile any code with those libs in Xcode, I get this:
'boost/scoped_ptr.hpp' file not found with include; use "quotes" instead'
and many errors alike for boost and mysql-connector-c++. Obviosly, changing all <> to "" in source files is NOT a good idea, futhermore, it's extremely tiring.
How to fix this error?

Clang fails to find iostream. What should I do?

Earlier, I posed a related question.
I have the following program extracted from a large project in my Mac OS
#include <iostream>
int main(){
std::cout<<"hello"<<std::endl;
return 0;
}
Compiling it with Clang fails with the following error:
$ clang test.cpp
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
For information,
A) I have already installed xcode command line tools, using xcodeselect --install. But it seems iostream does not locate in the default search path of clang.
B) Using g++ instead of clang compiles the program. But in my problem, I am not allowed to use other compiler than clang, or to change the source program.
C) I can see workaround techniques, e.g, by tweaking the search path in .bashrc or with some symbolic link, etc. But I feel reluctant to use them, because it seems that I have an installation problem with my Clang and tweaking the path only helps to avoid one of these path issues.
clang and clang++ do different things. If you want to compile C++ code, you need to use clang++
Alternatively you can invoke c++ compiler directly by providing language name explicitely:
clang -x=c++

error: ‘fileno’ was not declared in this scope

I am running Cygwin on windows 8, attempting to compile the source code for a game I would like to mod. Unfortunately I am running into some errors while building involving the fileno function. After doing some googling It seems like the problem might have to do with c++11 support (I'm not really sure what this means). Most of the solutions people have found involve adding some option like -std=c++0x or -std=c++11 when compiling, but my attempts to add the options into the makefile have been unsuccessful, and I don't know if that's whats causing the problem anyways. I'll include the code snippet that's throwing the error and a link to the makefile as it is quite large. Any advice you could give me would be great.
code that throws error:
time_t file_modtime(FILE *f)
{
struct stat filestat;
if (fstat(fileno(f), &filestat))
return 0;
return filestat.st_mtime;
}
Link to Makefile
it is being hosted on github
EDIT: After getting some advice I poked around the makefile and found five instances where the -std option was used, playing around with them hasn't changed anything. Is the problem with my Cygwin configuration? I installed the packages I was told I would need in the installation guide for the game I am building.
Changing the -std=c*** in your makefile to -std=gnu++0x should fix your problem.
If you don't know what c++11 is you're most likely not using it anyway.
Also if you need c++11 support you can also do: -std=gnu++11 instead of -std=gnu++0x
For windows...
fileno() is deprecated: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/posix-fileno?view=vs-2017
use _fileno() instead: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fileno?view=vs-2017