Printing in the Cling interactive terminal

Hi,

I built stand alone cling on Ubuntu Linux version 20.04.1 using gcc 9.4.0. It seems to have built without issue.

If I run the following simple commands interactively, it prints “a = 9”.

< ****************** CLING ******************
< * Type C++ code and press enter to run it *
< * Type .q to exit *
< *******************************************
< [cling] #include <iostream> < [cling] int a = 9;
< [cling]$ std::cout << "a = " << a << “\n”;

< a = 9
< [cling]$ .q

If I put these exact commands in a file called test.cpp, and load test.cpp with .L it causes errors because cout is not within the scope of main or a function.

< ****************** CLING ******************
< * Type C++ code and press enter to run it *
< * Type .q to exit *
< *******************************************
< [cling]$ .L test.cpp
< In file included from input_line_3:1:
< /home/ubuntu/software/cling/junk/test.cpp:3:6: error: no type named ‘cout’ in namespace ‘std’
< std::cout << "a = " << a << “\n”;

< ~~~~~^
< /home/ubuntu/software/cling/junk/test.cpp:3:11: error: expected unqualified-id
< std::cout << "a = " << a << “\n”;
< ^
< [cling]$ .q

If the instructions are put in the body of main, nothing is printed. If these same lines are put in a file and loaded with .L the result the same, nothing printed.

< ****************** CLING ******************
< * Type C++ code and press enter to run it *
< * Type .q to exit *
< *******************************************
< [cling] #include <iostream> < [cling] int main(int argc, char** argv) {
< [cling] ? int a = 9; < [cling] ? std::cout << "a = " << a << “\n”;
< [cling]$ ? }

< [cling]$ .q

I also tried:
.> test.out
.L test2.cpp
It created an empty file.

Is this the expected behavior? Is there some way to make it print?

Thanks,

Gene

From what I can see, you “load” the code (i.e., you “define” some functions) but you do not “execute” it (i.e., you do not “call” your functions).

First, my apology for not posting this in the newbie section. That would have made more sense.

Second, I’ve continued to work with Cling and see that you can call your functions. I’m adding to this post for the benefit of anyone new to Cling who has the same question as I did.

Running interactively, the .x command is used, not .L. Given the file test.cpp which contains:

#include <iostream>
#include <fstream>
#include <string>

// Print the integer passed into the subroutine.
void prnt_num(int num) {
  std::cout << "Number = " << num << ".\n";
}

// Function that returns the square of the number.
int sq_num(int num) {
    return num * num;
}

// Write to file
void write_file(std::string file_name, int num) {

    FILE* out_file = fopen(file_name.c_str(), "w");
    if(!out_file) {
        std::cerr << "\nCannot open file named: " << file_name << ".\n\n";
        exit (EXIT_FAILURE);
    }

    fprintf(out_file, "Number = %d", num);
    fclose(out_file);
}

void test() {
    int i = sq_num(11);
    write_file("foobar.txt", i);
    prnt_num(i);
}

Cling both prints the number and writes it to file foobar.txt

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ .x test.cpp
Number = 121.
[cling]$ .q

This also works if the functions are compiled into a library. Given the file example.cpp which is compiled into libexample.so

#include <stdio.h>
#include <fstream>
#include <string>
#include "example.h"

// Print the integer passed into the subroutine.
void prnt_num(int num) {
  std::cout << "Number = " << num << ".\n";
}

// Function that returns the square of the number.
int sq_num(int num) {
    return num * num;
}

// Write to file
void write_file(std::string file_name, int num) {

    FILE* out_file = fopen(file_name.c_str(), "w");
    if(!out_file) {
        std::cerr << "\nCannot open file named: " << file_name << ".\n\n";
        exit (EXIT_FAILURE);
    }

    fprintf(out_file, "Number = %d", num);
    fclose(out_file);
}

And file test2.cpp which contains:

void test2() {
    int i = sq_num(11);
    prnt_num(i);
    write_file("foobar.txt", i);
}

Calling Cling with the example library and appropriate header files both prints the number and writes it to file foobar.txt

$ ../obj/bin/cling -L/home/ubuntu/software/cling/test -lexample -I/home/ubuntu/software/cling/test -include string -include example.h 

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ .x test2.cpp
Number = 121.
[cling]$ 
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.