1
votes

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.

2
Please pay attention to the formatting of the error message: it's very hard to see anything clearly as it is presented. However, it looks like you are compiling/linking with g++ rather than gfortran?francescalus
Thanks, @francescalus . I edited my problem. Is that a better way to write? Also how do I fix that it is compiling using g++ instead of gfortran?Sky
Better, thanks. Could also try as code block rather than quote formatting, but this is much more readable. As to using g++ to link (and the two object files), which is now more obvious, we'll need someone knowledgeable of the IDE.francescalus
@francescalus since you are so proficient in Fortran (I saw your profile :) ), I was wondering if you could tell me how do I get fortran to start learning. The above way I did was followed this youtuber who showed how to install Fortran: youtube.com/watch?v=wGv2kGl8OV0Sky
@Ska I'm wondering whether you have gfortran properly installed. It looks like you are running Windows, so what do you see if you open a Command Prompt window and enter gfortran --version?skomisa

2 Answers

1
votes

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:

  • In the Projects window select your project, right click and select Properties from the context menu to open the Project Properties window.
  • Select Build > Linker from the Categories list.
  • On the right side of the Project Properties window click the ... button for Libraries.
  • In the Debug - Libraries window click the Add Option... button.
  • In the Select Option window:
    • Click the Other Option radio button.
    • Enter -lgfortran in the text field and press OK.
  • Click OK to close Debug - Libraries window. You should now see the -lgfortran option displayed in the Project Properties window:

    linkerSettings

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)
0
votes

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 :-)