Gfortran does not compile a simple 'hello world' program - fortran

i have a problem that is driving me crazy. I just did a clean installation of UBUNTU 18.04 LTS and installed VS Code and gfortran-9 successfully. The problem is that i can't compile a simple hello world program.
The file is saved as hello.f90 and i tried to compile it in several ways, like:
`gfortran-9 hello.f90 -o hello.exe
`gfortran-9 hello.exe
or using object file. I always end up with the same error:
/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
the code is simply:
program hello
implicit none
print *, 'hello world'
end program hello
I searched everywhere on the internet, but since i'm new to ubuntu when it comes to solution like: "installing libraries", or similar i just can't go on without anyone who explain me step by step...I hope in you.

I solved it. Basically i was trying to execute the executable with the command gfortran namefile.exe but then i realised that maybe i should've used the command ./namefile.exe and in fact it worked. So the compiling process was good, it was me who was wrong in the execution.

Related

First compilation of Fortran 90 with flang in FreeBSD 12

I am trying to get a 'hello world' type program compiled in FreeBSD 12 with flang.
This is my source code:
PROGRAM MAIN
INTEGER :: X
PRINT *, "Please, enter a number"
READ (*, *) X
PRINT *, "The square root of ", X, " is ", SQRT(X)
END PROGRAM MAIN
I try to compile it without success using:
$ flang -o test test.f90
/usr/local/bin/ld: /tmp/test-8e54ee.o: in function `MAIN_':
/usr/home/user/test/test.f90:6: undefined reference to `sqrt_'
/usr/local/bin/ld: /usr/local/flang/lib/libflangrti.so: undefined reference to `backtrace_symbols'
/usr/local/bin/ld: /usr/local/flang/lib/libflangrti.so: undefined reference to `backtrace'
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
It has been a long time since last time I used Fortran and it is definitively the first time I try to compile it with FreeBSD. Any help/hint is welcomed.
I was able to get around the first problem ("undefined reference to sqrt_") by declaring X as
REAL :: X
This makes sense, because SQRT in Fortran is defined for real numbers (otherwise it is not clear what KIND the returned REAL result would have), so flang does not resolve the call and expects that it is a reference to your own custom function defined somewhere else in the code.
As for the second problem ("undefined reference to backtrace_symbols"), this seems to me as a mess in installation. I have just installed a clean FreeBSD 12 into VirtualBox and the linker is in "/usr/bin/ld", and this is where it is looked for by flang, as apparent from the verbose output:
$ flang -o test test.f90
(...)
"/usr/bin/ld" --eh-frame-hdr -dynamic-linker /libexec/ld-elf.so.1 (... etc ...)

V8 "Hello World" Example - Compiling on Ubuntu 13.10 - tons of undefined reference errors "icu_46"

I am trying to follow the basic "hello world" example for Google's V8 as found here. I'm on Ubuntu 13.10, gcc version 4.8.1; this should be straighforward, no?
After building v8 itself and creating the hello world .cpp file,
I run (exactly** as Google suggests)
** Update: Okay, per my comments on the accepted answer below, I unwittingly wasn't running it exactly as Google suggests, because I thought the braces in the file name were an instruction to the reader to choose one option, not a
syntax that g++ would understand. Nontheless, it still doesn't work except with the additions suggested in the answer below
g++ -Iinclude helloworld.cpp -o hello_world out/x64.debug/obj.target/tools/gyp/libv8_base.x64.a -lpthread
And get bombarded with compile errors. There are too many to print here, but nearly all the errors are of the form:
undefined reference to 'icu_46 ...'
for example (some sample lines)
/home/ray/Playground/v8/out/../src/i18n.cc:138: undefined reference to `icu_46::UnicodeString::~UnicodeString()'
/home/ray/Playground/v8/out/../src/i18n.cc:125: undefined reference to `icu_46::UnicodeString::~UnicodeString()'
/home/ray/Playground/v8/out/../src/i18n.cc:147: undefined reference to `icu_46::UnicodeString::~UnicodeString()'
After much Googling I can figure out this has something to do with something called ICU (see http://userguide.icu-project.org/howtouseicu) but why I'm getting the error, and what I can do about it, I don't know.
While the majority of errors are icu_46 related, there are some like this as well, from the 'bootstrapper.o':
/home/ray/Playground/v8/out/x64.debug/obj.target/v8_base.x64/src/bootstrapper.o: In function `v8::internal::Bootstrapper::NativesSourceLookup(int)':
/home/ray/Playground/v8/out/../src/bootstrapper.cc:77: undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetBuiltinsCount()'
/home/ray/Playground/v8/out/../src/bootstrapper.cc:81: undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetRawScriptSource(int)'
/home/ray/Playground/v8/out/x64.debug/obj.target/v8_base.x64/src/bootstrapper.o: In function `v8::internal::Genesis::CompileBuiltin(v8::internal::Isolate*, int)':
/home/ray/Playground/v8/out/../src/bootstrapper.cc:1448: undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetScriptName(int)'
I'm Googled endlessly on this and have found signs that other people have run into the issue but no solutions. Any help will be hugely appreciated. Thank you.
I had the same problem on Ubuntu 12.04, g++ 4.6.3 and v8 3.22.18.
To compile hello_world.cc successfully it is necessary to add icu .a libraries and link to rt library too. As you have built v8 with make x64.debug your completed command should be:
g++ -Wall -Iinclude -o hello_world hello_world.cc ./out/x64.debug/obj.target/tools/gyp/libv8_{base.x64,snapshot}.a ./out/x64.debug/obj.target/third_party/icu/libicu{i18n,uc,data}.a -lrt

Wikiprep utility. Unable to Build Splitwiki

I am trying to build the Wikiprep utility on my system. While I have built the utility, I am unable to build the Splitwiki utility. The make install instruction is
splitwiki: splitwiki.c
gcc -Wall -O2 -lz -o $# $<
I get the following error messages upon execution:
/tmp/ccXeAIs1.o: In function `split':
splitwiki.c:(.text+0xac): undefined reference to `gzputs'
splitwiki.c:(.text+0xe3): undefined reference to `gzputs'
/tmp/ccXeAIs1.o: In function `main':
splitwiki.c:(.text.startup+0xab): undefined reference to `gzopen'
splitwiki.c:(.text.startup+0x10c): undefined reference to `gzclose'
collect2: ld returned 1 exit status
make: *** [splitwiki] Error 1
I somehow have my gut saying I am missing some other packages, although I am not sure about it.
Your help is greatly appreciated. Thanks.
gzputs() is part of zlib. You're probably not linking against zlib.
At the moment I'm wondering, how you're building splitwiki. I'd have expected it being build with 'make && make install' when building wikiprep. In my case it isn't. Can you give te commandline you're using to build splitwiki and all the output?
Update: The following works for me:
$ cd tools/splitwiki
$ make splitwiki LOADLIBES=-lz
cc splitwiki.c -lz -o splitwiki # this is make output
$
Explanation: I could not find any part of the Makefile handling or any part of the README explainigg or directing how to build splitwiki. My assumption is, that one simply needs to do it manually and that's the way you tried it todo. The thing you probably have been missing was '-lz' (link against zlib). You could have used
cc splitwiki.c -lz -o splitwiki
directly. Using make is only my way to invoke the compiler :-).
Update 2: Sorry, yesterday I seem to have missed the part of the question saying "the make install instruction is". So you seem to have gotten the -lz switch. NOrmally I'd say install zlib, but I'm a bit confused that you don't get '/usr/bin/ld: cannot find -lz' as error message. Are you sure you're giving all the log output relating to the compilation of splitwiki?

Trying to understand linking procedure for writing Python/C++ hybrid

I want to start learning more about using SWIG and other methods to interface Python and C++. To get started, I wanted to compile this simple program mentioned in another post:
#include <Python.h>
int main()
{
Py_Initialize();
PyRun_SimpleString ("import sys; sys.path.insert(0, '/home/ely/Desktop/Python/C-Python/')");
PyObject* pModule = NULL;
PyObject* pFunc = NULL;
pModule = PyImport_ImportModule("hello");
if(pModule == NULL){
printf("Error importing module.");
exit(-1);
}
pFunc = PyObject_GetAttrString(pModule, "Hello");
PyEval_CallObject(pFunc, NULL);
Py_Finalize();
return 0;
}
where the file "hello.py" just has the contents:
def Hello():
print "Hello world!"
Note: I already have python2.7-dev and python-dev and libboost-python-dev installed. But when I go to compile the code, I get errors that I believe are due to incorrectly linking to the Python libraries.
ely#AMDESK:~/Desktop/Python/C-Python$ gcc -I/usr/include/python2.7 test.cpp /tmp/ccVnzwDp.o: In function `main':
test.cpp:(.text+0x9): undefined reference to `Py_Initialize'
test.cpp:(.text+0x23): undefined reference to `PyImport_ImportModule'
test.cpp:(.text+0x58): undefined reference to `PyObject_GetAttrString'
test.cpp:(.text+0x72): undefined reference to `PyEval_CallObjectWithKeywords'
test.cpp:(.text+0x77): undefined reference to `Py_Finalize'
/tmp/ccVnzwDp.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
I was fishing around for examples of this online, and I found the following syntax, which causes the code to compile into an object file, but then I am unable to actually execute the file.
ely#AMDESK:~/Desktop/Python/C-Python$ gcc -c -g -I/usr/include/python2.7 test.cpp
ely#AMDESK:~/Desktop/Python/C-Python$ ./test.o
bash: ./test.o: Permission denied
ely#AMDESK:~/Desktop/Python/C-Python$ chmod ug=rx ./test.o
ely#AMDESK:~/Desktop/Python/C-Python$ ./test.o
bash: ./test.o: cannot execute binary file
ely#AMDESK:~/Desktop/Python/C-Python$ sudo chmod ug=rx ./test.o
ely#AMDESK:~/Desktop/Python/C-Python$ ./test.o
bash: ./test.o: cannot execute binary file
The same behavior as above is still seen if I use g++ instead of gcc.
Help in understanding my error in linking would be great, and even better for any sort of explanation that helps me understand the "logic" behind the kind of linking I need to do, so that I'll remember better what possible things I am forgetting the next time. Thanks!
What you are seeing are linker errors. To fix those, you need to link python2.7 library.
Try next line :
gcc -I/usr/include/python2.7 test.c -lpython2.7
it should work.
First, do you compile C or C++ code?
Use gcc for the former, and g++ for the latter. C++ code needs some additional linking to be performed.
Second: you have to link your program to libpython2.7.so to embed the interpreter into it. To do this, add -lpython2.7 to gcc command line.
test.o is not your executable file, that's why you can't execute it.
The default name for your program is a.out, try running that. You can specify a name for your program using the -o option.

Haskell compilation problem

I have a problem compiling Haskell programs, that import the Text.Regex.Posix module. I have tried to isolate the problem in a small test program:
module Main () where
import Text.Regex.Posix ((=~))
main = return ()
Running the interpreter works fine:
/regex-test$ runghc Main.hs
/regex-test$
However, compiling this program with ghc yields:
/regex-test$ ghc -o tester Main.hs
Main.o: In function `rmS_info':
(.text+0xf3): undefined reference to `__stginit_regexzmposixzm0zi72zi0zi3_TextziRegexziPosix_'
collect2: ld returned 1 exit status
After this, the interpreter also stops working:
/regex-test$ runghc Main.hs
<interactive>:1:32: Not in scope: `main'
/projects/regex-test$
I can get it to work again by saving the file again without making any changes.
Does anyone have an idea how to solve this?
Some info on my system:
/regex-test$ uname -a
Linux Hello-Ubuntu 2.6.28-15-generic #52-Ubuntu SMP Wed Sep 9 10:49:34 UTC 2009 i686 GNU/Linux
and
/regex-test$ ghc-pkg list
/usr/local/lib/ghc-6.10.3/./package.conf:
Cabal-1.6.0.3, HUnit-1.2.0.3, QuickCheck-1.2.0.0, array-0.2.0.0,
base-3.0.3.1, base-4.1.0.0, bytestring-0.9.1.4, containers-0.2.0.1,
directory-1.0.0.3, (dph-base-0.3), (dph-par-0.3),
(dph-prim-interface-0.3), (dph-prim-par-0.3), (dph-prim-seq-0.3),
(dph-seq-0.3), extensible-exceptions-0.1.1.0, filepath-1.1.0.2,
(ghc-6.10.3), ghc-prim-0.1.0.0, haddock-2.4.2, haskell-src-1.0.1.3,
haskell98-1.0.1.0, hpc-0.5.0.3, html-1.0.1.2, integer-0.1.0.1,
mtl-1.1.0.2, network-2.2.1, old-locale-1.0.0.1, old-time-1.0.0.2,
packedstring-0.1.0.1, parallel-1.1.0.1, parsec-2.1.0.1,
pretty-1.0.1.0, process-1.0.1.1, random-1.0.0.1,
regex-base-0.72.0.2, regex-base-0.93.1, regex-compat-0.71.0.1,
regex-posix-0.72.0.3, regex-tdfa-1.1.2, rts-1.0, stm-2.1.1.2,
syb-0.1.0.1, template-haskell-2.3.0.1, time-1.1.3, unix-2.3.2.0,
xhtml-3000.2.0.1
~/.ghc/i386-linux-6.10.3/package.conf:
HTTP-4000.0.4, zlib-0.5.0.0
I'd expect at least main to be exported... i.e.
module Main (main) where
but that's not the problem here.
$ ghc -o tester -package regex-posix Main.o
Linking like this works perfectly fine.
You should either use cabal as your buildsystem, compile and link with ghc --make, or take care to expose all requisite packages manually.
Do exactly what you did but pass --make to ghc as well.
You're missing --make flag, which has GHC solve the missing library dependencies for you.