mingw C++ won't compile j0 funciton - windows

I'm trying to compile a program on Windows using MingW (msys2) and it fails with the j0 function. On Linux it compiles no problem. It seems to hate when I use the -std=c++11 flag on the compiler. How can I get this to compile properly and with the -std=c++11 flag on?
Sample code:
#include <cmath>
int main( int argc, char *argv[] )
{
float test = j0( 5 );
}
Output
$ g++ -std=c++11 test.cpp -o test
test.cpp: In function 'int main(int, char**)':
test.cpp:6:21: error: 'j0' was not declared in this scope
float test = j0( 5 );

Apparently, MinGW defines the Bessel functions only when __STRICT_ANSI__ is not defined, and it is defined when -std=c++11 is specified. I was able to get your code to compile in MinGW by adding #undef __STRICT_ANSI__ at the top of the file. See https://sourceforge.net/p/mingw-w64/feature-requests/68/
You might also try -std=gnu++11 instead. See https://stackoverflow.com/a/19667112/10077

Related

How to compile c complex numbers with c++11

I am extending a project which compiles some libraries with c99 and one library with c++11 and uses complex.h complex numbers in the c99 library.
I know this may be a stupid idea but it wasn't mine and I need to make it work somehow.
The code does not compile with gcc4.9 and -std=c++11 and I am quite clueless what to do. How do I make this snippet compile?
#include <complex.h>
#include <cstdio>
int main(int argc, char * argv[]) {
double _Complex a = 3.0 + 0.0 * _Complex_I;
//printf("%lf\n", creal(a));
return 0;
}
Gives the error
In file included from /usr/local/include/c++/7.1.0/complex.h:36:0,
from main.cpp:1:
main.cpp: In function 'int main(int, char**)':
main.cpp:5:33: error: unable to find numeric literal operator 'operator""iF'
double _Complex a = 3.0 + 0.0 _Complex_I;
^
main.cpp:5:33: note: use -std=gnu++11 or -fext-numeric-literals to enable more built-in suffixes
main.cpp:5:33: error: expression cannot be used as a function
double _Complex a = 3.0 + 0.0 _Complex_I;
^
main.cpp:5:19: warning: unused variable 'a' [-Wunused-variable]
double _Complex a = 3.0 + 0.0 _Complex_I;
with
g++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
-std=gnu++11 works it seems, but is there a way to make it work with -std=c++11?
How to compile c complex numbers with c++11
You don't. Standard C++ does not support the <complex.h> C library header.
Either:
Use the GCC language extensions.
Implement a wrapper function for anything that deals with C complex numbers that provides an interface which doesn't use C complex numbers. Implement the wrapper in C and use the wrapper from C++.
Don't use C complex numbers in the first place - use std::complex instead.

Linking gnu libraries c++ and fortran

I have spent the day searching for an answer to what should be a simple problem. I am building a c++ program to call a fairly large amount of existing fortran. I started by changing the fortran main to a subroutine and then called it with a simple c++ main. My steps look like:
gfortran -c f1.f90 f2.f90 ......
g++ -c mn.cpp
gfortran -lstdc++ -o prog.exe mn.o f1.o ....
mn.cpp started out looking like the code below, and the above steps do work ok. I get a host of linker errors if I try to link with:
g++ -lgfortran (this never works!)
Next, I tried to instantiate a simple array class (remove the 2 commented lines). This produced linker errors concerning gxx_personality_seh0, vtable, and operator new.I get similar errors if I just create an array of doubles with new (remove comment), and if I remove the call to the fortran program completely (still linking with gfortran). Obviously, -lstdc++ does not bring in all the libraries needed. Which libraries are needed and how can I get it to link them?
I am using Windows 7 with Cygwin. The libraries it links are in ...lib/x86_64-pc-cygwin/4.9.3. I can post the linker output if it would be helpful.
mn.cpp which works (code commented out) is below:
#include <string.h>
#include <stdlib.h>
//#include "array.h"
extern "C" {
void mnf90_(const char*,int);
}
int main(int argc, char* argv[]){
// Array2D A; // first derivative
static const char *feos = "d/fld9x.dat";
int npoint = 20;
// double *xc = new double[npoint];
mnf90_(feos,strlen(feos));
}

g++ 5.4.0 - unable to use C++14 standard

I installed gcc 5.4.0 recently, on Windows using Cygwin, because I wanted to test the C++14 standard features of g++. When I tried to compile, I get the following error:
$ g++-5.4.0 -std=c++14 test.cpp
-bash: g++-5.4.0: command not found
This is the code I wrote inside test.cpp:
#include <iostream>
int main()
{
auto lambda = [](auto x){ return x; };
std::cout << lambda("Hello generic lambda!\n");
return 0;
}
What could be the problem? I also tried replacing C++14 with C++11 in the command, but got the same error.
When Cygwin installs a g++ version (in your case, 5.4.0), it will place the g++ executable in your PATH variable. But the installation name is just g++.exe, so you can call the program like this:
g++ -std=c++14 test.cpp
If you really wanted to call the compiler with g++-5.4.0, you could symlink the actual g++ executable to that name:
ln -s /usr/bin/g++.exe /usr/bin/g++-5.4.0.exe
then you will be able to call the program from the command line with either g++ or g++-5.4.0:
g++-5.4.0 -std=c++14 test.cpp
g++ -std=c++14 test.cpp

calling functions from a different c file

actually I was compiling with multiple files. Following are the files:
file main.c -->
#include <stdio.h>
void foo3(void)
{
printf("INSIDE foo3 function\n");
}
int main()
{
foo1();
foo2();
foo3();
}
file 1.c -->
#include <stdio.h>
void foo1(void)
{
printf("INSIDE foo1 function\n");
}
file 2.c-->
#include <stdio.h>
void foo2(void)
{
printf("INSIDE foo2 function\n");
}
Now I compiled using gcc as follows-->
gcc 1.c 2.c main.c -o main
following was the output -->
INSIDE foo1 function
INSIDE foo2 function
INSIDE foo3 function
My doubt is how could main() call foo1() and foo2() when they are not declared in main.c. But now if I change main.c as follows ( writing the definition of foo3() after main()) like this:
edited main.c -->
#include <stdio.h>
int main()
{
foo1();
foo2();
foo3();
}
void foo3(void)
{
printf("INSIDE foo3 function\n");
}
and then if I compile I get this error:
main.c:9:6: warning: conflicting types for ‘foo3’ [enabled by default]
void foo3(void)
^
main.c:6:2: note: previous implicit declaration of ‘foo3’ was here
foo3();
^
why was this error not shown earlier in case of foo1() and foo2() . Thankyou in advance.
My doubt is how could main() call foo1() and foo2() when they are not declared in main.c
Because the GCC compiler defaults to the old ANSI C (aka as C89) language, where undeclared functions are permitted and defaults to giving int result.
Try to invoke the compiler as e.g.
gcc -std=c99 -Wall -g -c main.c
or (if you want to compile all files at once)
gcc -std=c99 -Wall -g 1.c 2.c main.c -o main
You could ask for link time interprocedural optimizations with gcc -flto instead of gcc using a recent GCC, e.g. GCC 4.9 in september 2014.
This would want a C99 conforming source code where all functions should be declared.
The -Wall asks for (almost) all warnings. The -g option produces a debuggable object code (or executable for the last command compiling all files at once).
In your edited main.c when foo3 first occurrence (inside main) is encountered, the compiler guesses that it is a function returning int. When the compiler sees the definition of foo3 it rightly complains.
You could use the -Wstrict-prototypes warning option to gcc (but it is implied by -Wall which I always recommend using).
At link time, the type (and signature) of C functions does not matter. The linker just uses name to do its job (but C++ use name mangling). Of course, calling a function with the incorrect arguments or result is undefined behavior.
The good conventional practice is to have a common header file declaring all the used and public functions and types (and constants) and include that header file in your source files (this avoids to have to copy and paste these declarations several times). So you whould have a new header file myheader.h like
// file myheader.h
#ifndef MY_HEADER_INCLUDED
#define MY_HEADER_INCLUDED
void foo1(void);
void foo2(void);
void foo3(void);
#endif /*MY_HEADER_INCLUDED*/
and you would add #include "myheader.h" in all your source files (after the #include <stdio.h> directive there). Notice the include guard trick with MY_HEADER_INCLUDED.
In practice, header files usually contain comments explaining the API of your program.
Learn also about GNU make. It will ease the building of your multi-source code files programs (you just compile and build by running make). See this and that examples of Makefile. Understand that C preprocessing is the first phase of C compilation.

Problem compiling program using fork in cygwin

I am trying to run a simple program in cygwin that includes fork and wait.
I thought it would be very easy to compile but I am having problems.
#include <stdio.h>
#include <unistd.h>
void testFork(){}
int main(int argc,char* argv[]){
if (fork()==0) {testFork();return 0;}
while (wait() == -1);
return 0;
}
Compiled using:
gcc -Wall -Wextra -o test.o test
I get the following error:
C:\Users\Aaron\AppData\Local\Temp\ccgh3MfS.o:ostest.c:(.text+0x11): undefined reference to `fork'
C:\Users\Aaron\AppData\Local\Temp\ccgh3MfS.o:ostest.c:(.text+0x22): undefined reference to `wait'
collect2: ld returned 1 exit status
I'm sure I'm missing something trivial. Any ideas?
The linker can't find the standard C libraries.
Did you install Cygwin in the normal way? (Here's a simple guide: http://www.eecg.utoronto.ca/~aamodt/ece242/cygwin.html).
Have you been able to compile even simpler programs:
#include <stdio.h>
int main(int argc, char **argv) {
printf("Found C library.\n");
}
If that doesn't compile, you might just want to try removing and reinstalling Cygwin - something is broken.
C:\Users\Aaron\AppData\Local\Temp\ccgh3MfS.o is a Windows-style path. If you're using Cygwin, you only be seeing Cygwin-style paths, perhaps something like /cygdrive/C/Users/Aaron/AppData/Local/Temp/ccgh3MfS.o.
You said your command line was
gcc -Wall -Wextra -o test.o test
but it was probably
gcc -Wall -Wextra -o test.o test.c
Are you invoking gcc from the Cygwin command line? What does type gcc say?
It seems that MinGW gcc is being invoked because the cygwin gcc package is not installed.
You can verify that by calling the "cygcheck -c" in the cygwin commandline which will list all the installed packages, if you can't find the gcc in the list you need to install it

Resources