# Use of undeclared identifier

**URL:** <https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202>\
**Category:** Cling\
**Tags:** root\
**Created:** [November 21, 2023, 11:18pm UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202 "2023-11-21T23:18:36Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![cw2636](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@cw2636](https://root-forum.cern.ch/u/cw2636)\
**Post date:** [November 21, 2023, 11:18pm UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/1 "2023-11-21T23:18:36Z")

</div>

After successful installing cling. I need to test the interpreter part of it. I create a simple script which is included at the end for some reason i am getting this error.

input\_line\_9:5:15: error: use of undeclared identifier ‘strDir’  
if ( stat ( strDir.c\_str ( ), &st ) == 0 )  
^  
input\_line\_9:7:11: error: use of undeclared identifier ‘strDir’  
mkdir ( strDir.c\_str ( ), mode );  
^  
input\_line\_11:4:4: error: use of undeclared identifier ‘mkDir’  
mkDir(dir);  
^  
script.cxx:

```auto

#include <iostream>
#include <unistd.h>
#include <sys/stat.h>
#include <ctype.h>

using namespace std;

void mkDir ( string strDir )
{
  mode_t mode = 0777;
  struct stat st;
  if ( stat ( strDir.c_str ( ), &st ) == 0 )
    return; // directory exists.

  mkdir ( strDir.c_str ( ), mode );
}

int main ( int argc, char *argv[] )
{
   string dir = "/testing";
   mkDir(dir);
   std::cout << "Resetting remote " << std::endl;
 
  exit ( 0 );
}

```

Please help

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 22, 2023, 6:53am UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/2 "2023-11-22T06:53:31Z")

</div>

Very strange. How do you run this code in cling?

---

<div class="post-metadata">

**Author:** ![cw2636](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@cw2636](https://root-forum.cern.ch/u/cw2636)\
**Post date:** [November 22, 2023, 2:04pm UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/3 "2023-11-22T14:04:02Z")

</div>

It is a script. I just run the script ./script, even cling .x script yields same error

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 23, 2023, 8:04am UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/4 "2023-11-23T08:04:32Z")

</div>

How do you get from `script.cxx` to `./script`?

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 23, 2023, 11:46am UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/6 "2023-11-23T11:46:20Z")

</div>

@cw2636 I cannot reproduce this:

```auto
> ./inst/bin/cling ./script.cxx 
warning: Failed to call `script()` to execute the macro.
Add this function or rename the macro. Falling back to `.L`.

```

is 100% of what is expected to happen. Most importantly there’s no syntax error here, loading that file works just fine. So - what are you doing differently?

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 23, 2023, 11:51am UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/7 "2023-11-23T11:51:26Z")

</div>

OK found it:

```auto
cat script.cxx |./inst/bin/cling

******************CLING******************
* Type C++ code and press enter to run it *
* Type .q to exit *
*******************************************
input_line_9:5:15: error: use of undeclared identifier 'strDir'
  if ( stat ( strDir.c_str ( ), &st ) == 0 )
              ^
input_line_9:7:11: error: use of undeclared identifier 'strDir'
  mkdir ( strDir.c_str ( ), mode );
          ^
input_line_11:4:4: error: use of undeclared identifier 'mkDir'
   mkDir(dir);
   ^

```

In this mode, each line is taken as separate input. `cling` sees `void mkDir ( string strDir )` - and interprets this as a declaration where you left out the training `;`. The next line `{` then just opens a scope - but _not_ a function scope. I.e. you can think of `cling` interpreting you code as:

```c++
void mkDir ( string strDir );
{
  mode_t mode = 0777;
  struct stat st;
  if ( stat ( strDir.c_str ( ), &st ) == 0 )

```

and now you see where the errors come from. The workaround is dead trivial: add the `{` after the function prototype, such that cling knows that input will continue:

```c++
void mkDir ( string strDir ) {
  mode_t mode = 0777;

```

Also note that `main` is _not_ called by `cling`; for `.x` you can use a function with the same name as a file; for pasting lines into cling you need to invoke a function explicitly:

```auto
void someFunc() {
...
}
someFunc();

```

And then all of a sudden things work:

```auto
$ cat script.cxx |./inst/bin/cling

******************CLING******************
* Type C++ code and press enter to run it *
* Type .q to exit *
*******************************************
Resetting remote 

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 23, 2023, 7:01pm UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/8 "2023-11-23T19:01:25Z")

</div>

@Axel This reminds me that I also often face this problem, usually when I “copy & paste” some piece of source code, e.g.:

```c++
for (int i = 0; i < 1; i++)
  cout << i << "\n";

```

So, could you please DISABLE this feature so that a trailing “`;`” is NOT added automatically?

---

<div class="post-metadata">

**Author:** ![cw2636](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@cw2636](https://root-forum.cern.ch/u/cw2636)\
**Post date:** [November 23, 2023, 9:11pm UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/9 "2023-11-23T21:11:02Z")

</div>

Thanks. This was helpful

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 24, 2023, 6:43am UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/10 "2023-11-24T06:43:46Z")

</div>

We don’t do this for fun and profit but because of `myFunc(x)+42` where people want to get the result printed. But it’s not valid C++, and to survive the liblang parsing we just always add a semicolon, unconditionally.

I predict we would also have tons of people complaining why they now have to add a `;` after `int x=42` but not after `x=42` (which needs to print `(int&) 42`).

Maybe changing the parsing in cling - once we can reliably distinguish expressions from declarations before we add the training semicolon - could help, but I currently don’t see how to escape the user expectation outlined above.

Ideas?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 24, 2023, 9:04am UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/11 "2023-11-24T09:04:47Z")

</div>

In such cases, I can often help myself if I “encapsulate” the piece of source code in a “{ … }” pair (so, I create a tiny unnamed macro). Unfortunately, this often breaks, too.

There is a nasty unsolved problem with “unnamed macros”, known since 11 years ago: “[`Return from an unnamed macro in ROOT 6 broken`](https://root-forum.cern.ch/t/return-from-an-unnamed-macro-in-root-6-broken/29867)”

Maybe when you finally fix [this problem](https://root-forum.cern.ch/t/return-from-an-unnamed-macro-in-root-6-broken/29867), also the artificial “`;`” will not be needed anymore.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 24, 2023, 9:19am UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/12 "2023-11-24T09:19:58Z")

</div>

Sorry Wile\_E I have _no_ idea what’s connecting these two problems? Could you explain / expand? Returns from unnamed macros vs what I explained above regarding `int i = 42` vs `i = 42` - what’s the connection? The fact that you enclose code in `{}` to hack around the declaration vs expression / continuation issue? Well, that’s just another workaround then, and `int myfunc() {` feels strictly superior as a workaround. Anyway I’d rather see us fix the underlying `;` issue! But as I said: I need ideas how to maneuver user expectations - right now I don’t see how we could pull this off!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 24, 2023, 9:33am UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/13 "2023-11-24T09:33:33Z")

</div>

> [@Return from an unnamed macro in ROOT 6 broken](https://root-forum.cern.ch/t/return-from-an-unnamed-macro-in-root-6-broken/29867/3):
>
> Could you, please, increase the “Priority” for this jira report. Named macros are not a replacement for the unnamed ones. One needs all variable which are created in unnamed macros to be still accessible from the ROOT prompt (global namespace). And then the user and/or another unnamed macros are supposed to be able to modify these variables.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 24, 2023, 10:07am UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/14 "2023-11-24T10:07:28Z")

</div>

Still not clear what the connection between adding `;` or not to function declarations and missing return for unnamed macros is.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [December 8, 2023, 10:08am UTC](https://root-forum.cern.ch/t/use-of-undeclared-identifier/57202/15 "2023-12-08T10:08:08Z")

</div>

Closed on December 8, 2023, 10:08am UTC

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