Compiling with
gcc test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:3:4: error: ‘cout’ was not declared in this scope
3 | cout << "Hello" << endl;
| ^~~~
test.cpp:1:1: note: ‘std::cout’ is defined in header ‘<iostream>’; did you forget to ‘#include <iostream>’?
+++ |+#include <iostream>
1 | int main() {
test.cpp:3:23: error: ‘endl’ was not declared in this scope
3 | cout << "Hello" << endl;
| ^~~~
test.cpp:1:1: note: ‘std::endl’ is defined in header ‘<ostream>’; did you forget to ‘#include <ostream>’?
+++ |+#include <ostream>
1 | int main() {
Compiling with
gcc test.cpp -o test
test.cpp:3:5: error: cannot declare ‘::main’ to be a global variable
3 | int main {
| ^~~~
test.cpp:4:4: error: expected primary-expression before ‘using’
4 | using namespace std;
| ^~~~~
test.cpp:4:4: error: expected ‘}’ before ‘using’
test.cpp:3:10: note: to match this ‘{’
3 | int main {
| ^
test.cpp:5:4: error: ‘cout’ does not name a type
5 | cout << "Hello" << endl;
| ^~~~
test.cpp:6:4: error: expected unqualified-id before ‘return’
6 | return 0;
| ^~~~~~
test.cpp:7:1: error: expected declaration before ‘}’ token
7 | }
| ^
Compiling with
gcc test.cpp -o test
/usr/bin/ld: /tmp/ccrbNm7k.o: warning: relocation against `_ZSt4cout' in read-only section `.text'
/usr/bin/ld: /tmp/ccrbNm7k.o: in function `main':
test.cpp:(.text+0x15): undefined reference to `std::cout'
/usr/bin/ld: test.cpp:(.text+0x1d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: test.cpp:(.text+0x24): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/usr/bin/ld: test.cpp:(.text+0x2f): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/usr/bin/ld: /tmp/ccrbNm7k.o: in function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x66): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: test.cpp:(.text+0x81): undefined reference to `std::ios_base::Init::~Init()'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
Setting compiler flags for compiled languages like Fortran and C
Check the manuals
Also gcc compile time checks and options like
-Wall and-WextraCompiling with
gcc inv_ptr.c -o inv_ptr
Compiler output
inv_ptr.c: In function ‘main’:
inv_ptr.c:8:4: warning: ‘free’ called on unallocated object ‘number’ [-Wfree-nonheap-object]
8 | free(ptr);
| ^~~~~~~~~
inv_ptr.c:4:8: note: declared here
4 | int number = 1;
| ^~~~~~
Run output e.g.
Segmentation fault
Compiling with
gcc inv_ptr.c -Werror -o inv_ptr
Compiler output
inv_ptr.c: In function ‘main’:
inv_ptr.c:8:4: error: ‘free’ called on unallocated object ‘number’ [-Werror=free-nonheap-object]
8 | free(ptr);
| ^~~~~~~~~
inv_ptr.c:4:8: note: declared here
4 | int number = 1;
| ^~~~~~
cc1: all warnings being treated as errors
Setting compiler flags for compiled languages like Fortran and C
Typically using -g for Intel, GCC and many other compilers
In the Fortran world, also
-fbacktrace, -fbounds-check-traceback, -check bounds, -check allTry with more than just one compiler
\(\Rightarrow\) HPC systems usually provide native compiler plus gcc/gfortan. Try both variants.
Using gdb to inspect a core dump
For core dumps, also man 5 core
core dumpprogram loop_count_2d
implicit none
integer, parameter :: leni = 3
integer, parameter :: lenj = 5
integer, parameter :: len = leni*leni
! ERROR: leni used twice for len
integer :: my_ij(2,len)
integer :: i, j, n, ii, jj
n = 0
do j = 1, lenj
do i = 1, leni
n = n+1
my_ij(1,n) = i
my_ij(2,n) = j
enddo
enddob <line>: Add breakpoint at given line numberinfo b: Show current breakpointsclear: Clear current breakpointd: Clear all breakpointsrun: Run the programl: Print codeinfo locals: Print value of local variablesp <var>: Print value of variableup, down or frame <num>: Navigate stackn: Next line (without descending)step: Next commandh: Show helpbt: Create backtracec: Continue executionq: Quit gdbFor this exercise, check out the Fortran code schnecke_flt.f90. You can compile it on Levante e.g. by loading GCC 11.2.0 compilers with
and then run
Task
gdb to locate the error. Document your approach.