Reputation: 55
I am trying to learn Fortran using the NetBeans IDE with the basic "Hello World" programming application. However, I keep getting "Build Failed". I don't know why? I have installed the MinGW. Then I configured the PATH in the Environment Variables. Finally, I installed NetBeans. This is what I programmed in NetBeans:
PRINT*, 'Hello World'
END
The following is the error and the history that I get:
cd 'C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1'
C:\MinGW\msys\1.0\bin\make.exe -f Makefile CONF=Debug
"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/c/Users/ABCD/Documents/NetBeansProjects/CppApplication_1'
"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/cppapplication_1.exe
make.exe[2]: Entering directory `/c/Users/ABCD/Documents/NetBeansProjects/CppApplication_1'
mkdir -p build/Debug/MinGW-Windows
gfortran -c -g -o build/Debug/MinGW-Windows/testfortran.o testfortran.f90
mkdir -p dist/Debug/MinGW-Windows
g++ -o dist/Debug/MinGW-Windows/cppapplication_1 build/Debug/MinGW-Windows/main.o build/Debug/MinGW-Windows/testfortran.o
build/Debug/MinGW-Windows/testfortran.o: In function `main':
C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/testfortran.f90:2: multiple definition of `main'
build/Debug/MinGW-Windows/main.o:C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/main.cpp:21: first defined here
build/Debug/MinGW-Windows/testfortran.o: In function `MAIN__':
C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/testfortran.f90:1: undefined reference to `_gfortran_st_write'
C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/testfortran.f90:1: undefined reference to `_gfortran_transfer_character_write'
C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/testfortran.f90:1: undefined reference to `_gfortran_st_write_done'
build/Debug/MinGW-Windows/testfortran.o: In function `main':
C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/testfortran.f90:2: undefined reference to `_gfortran_set_args'
C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/testfortran.f90:2: undefined reference to `_gfortran_set_options'
collect2.exe: error: ld returned 1 exit status
make.exe[2]: *** [dist/Debug/MinGW-Windows/cppapplication_1.exe] Error 1
make.exe[2]: Leaving directory `/c/Users/ABCD/Documents/NetBeansProjects/CppApplication_1'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/c/Users/ABCD/Documents/NetBeansProjects/CppApplication_1'
make.exe": *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
I am not understanding how do I fix it? I would appreciate any help as I am a beginner to the Fortran/C++ world.
Upvotes: 2
Views: 1142
Reputation: 1
You have created a C++ project then a fortran file. Your IDE created a main in c++ for you. I guess it havent in the tutorial you have seen. If i have seen it right then the answer is simple:
Just right click and delete this main.c and you are good :-)
Upvotes: 0
Reputation: 17383
I followed the video tutorial you linked to in your comment, and I got the same build errors. Two unrelated changes are needed to resolve them:
To resolve the multiple definition of 'main'
error, see the accepted answer to the Stack Overflow question gfortran multiple definition of main. Just copy and paste the source for the C++ and Fortran examples into your project files.
Rebuild your project and the multiple definition of 'main'
error should be gone.
However, that does not fix the undefined reference
errors. The solution for that is on the (old) NetBeans web site:
So, in the Linker window we when we add the reference to the Fortran library by bringing up the Libraries window one will notice an Add Options button on this page....here is where we put the -lgfortran option under Other Options. Presto, the C code will compile and doesn't complain about missing Fortran references.
The specific steps needed to fix the undefined reference
errors are:
...
button for Libraries.Other Option
radio button.Click OK to close Debug - Libraries window. You should now see the -lgfortran option displayed in the Project Properties window:
Having made the changes described above the projects builds without errors:
cd 'D:\NB82\CppApplication_5'
C:\msys\1.0\bin\make.exe -f Makefile CONF=Debug clean
"/C/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .clean-conf
make.exe[1]: Entering directory `/d/NB82/CppApplication_5'
rm -f -r build/Debug
rm -f *.mod
make.exe[1]: Leaving directory `/d/NB82/CppApplication_5'
CLEAN SUCCESSFUL (total time: 558ms)
cd 'D:\NB82\CppApplication_5'
C:\msys\1.0\bin\make.exe -f Makefile CONF=Debug
"/C/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/d/NB82/CppApplication_5'
"/C/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/cppapplication_5.exe
make.exe[2]: Entering directory `/d/NB82/CppApplication_5'
mkdir -p build/Debug/MinGW-Windows
rm -f "build/Debug/MinGW-Windows/main.o.d"
g++ -c -g -MMD -MP -MF "build/Debug/MinGW-Windows/main.o.d" -o build/Debug/MinGW-Windows/main.o main.cpp
mkdir -p build/Debug/MinGW-Windows
gfortran -c -g -Wall -o build/Debug/MinGW-Windows/newfortranFreeFormatFile.o newfortranFreeFormatFile.f90
mkdir -p dist/Debug/MinGW-Windows
g++ -o dist/Debug/MinGW-Windows/cppapplication_5 build/Debug/MinGW-Windows/main.o build/Debug/MinGW-Windows/newfortranFreeFormatFile.o -lgfortran
make.exe[2]: Leaving directory `/d/NB82/CppApplication_5'
make.exe[1]: Leaving directory `/d/NB82/CppApplication_5'
BUILD SUCCESSFUL (total time: 1s)
Running the Fortran project then produces the expected output:
main in C++
FortMain
RUN SUCCESSFUL (total time: 2s)
Upvotes: 1