# Failed to compile for codecrafters

**URL:** <https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [May 4, 2025, 1:24am UTC](https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906 "2025-05-04T01:24:25Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![devnchill](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/devnchill/32/7291_2.png) [@devnchill](https://forum.codecrafters.io/u/devnchill)\
**Post date:** [May 4, 2025, 1:24am UTC](https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906/1 "2025-05-04T01:24:25Z")

</div>

I’m stuck on Stage #QP2.

I’ve tried using readline and autocompletion works locally but while compiling it requires linking readline and histry but looks like cmake doesn’t works as it might not have that linked ?

Here are my logs:

```auto
 CodeCraftersCell git:(master) ✗ codecrafters submit
Submitting changes (commit: 32d1f65)...

Welcome back! Your first build could take slightly longer, please bear with us.
Subsequent ones will be snappy ⚡

[build] Starting build...
[build] If you don't see logs for 60s+, please contact us at hello@codecrafters.io
[build] Step 1 complete.
[build] Step 2 complete.
[build] Step 3 complete.
[build] Step 4 complete.
[build] Step 5 complete.
[build] Step 6 complete.
[build] Step 7 complete.
[build] Step 8 complete.
[build] Step 9 complete.
[build] Step 10 complete.
[build] Step 11 complete.
[build] Step 12 complete.
[build] Step 13 complete.
[build] Step 14 complete.
[build] Step 15 complete.
[build] Step 16 complete.
[build] Step 17 complete.
[build] > All requested packages are currently installed.
[build] > Total install time: 40.8 us
[build] Step 18 complete.
[build] Step 19 complete.
[build] Step 20 complete.
[build] Step 21 complete.
[build] Step 22 complete.
[build] Step 23 complete.
[build] Step 24 complete.
[build] Step 25 complete.
[build] Step 27 complete.
[build] Step 26 complete.
[build] Build successful.

Running tests. Logs should appear shortly...

[compile] -- Running vcpkg install
[compile] All requested packages are currently installed.
[compile] Total install time: 52.6 us
[compile] -- Running vcpkg install - done
[compile] -- The C compiler identification is GNU 14.2.0
[compile] -- The CXX compiler identification is GNU 14.2.0
[compile] -- Detecting C compiler ABI info
[compile] -- Detecting C compiler ABI info - done
[compile] -- Check for working C compiler: /usr/bin/cc - skipped
[compile] -- Detecting C compile features
[compile] -- Detecting C compile features - done
[compile] -- Detecting CXX compiler ABI info
[compile] -- Detecting CXX compiler ABI info - done
[compile] -- Check for working CXX compiler: /usr/local/bin/c++ - skipped
[compile] -- Detecting CXX compile features
[compile] -- Detecting CXX compile features - done
[compile] -- Configuring done (1.1s)
[compile] -- Generating done (0.0s)
[compile] -- Build files have been written to: /app/build
[compile] [50%] Building C object CMakeFiles/shell.dir/src/main.c.o
[compile] [100%] Linking C executable shell
[compile] /usr/bin/ld: CMakeFiles/shell.dir/src/main.c.o: in function `command_completion':
[compile] main.c:(.text+0x4ed): undefined reference to `rl_attempted_completion_over'
[compile] /usr/bin/ld: main.c:(.text+0x502): undefined reference to `rl_completion_matches'
[compile] /usr/bin/ld: CMakeFiles/shell.dir/src/main.c.o: in function `main':
[compile] main.c:(.text+0x527): undefined reference to `rl_attempted_completion_function'
[compile] /usr/bin/ld: main.c:(.text+0x535): undefined reference to `readline'
[compile] /usr/bin/ld: main.c:(.text+0x56c): undefined reference to `add_history'
[compile] collect2: error: ld returned 1 exit status
[compile] gmake[2]: *** [CMakeFiles/shell.dir/build.make:97: shell] Error 1
[compile] gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/shell.dir/all] Error 2
[compile] gmake: *** [Makefile:91: all] Error 2
[compile] Looks like your code failed to compile.
[compile] If you think this is a CodeCrafters error, please let us know at hello@codecrafters.io.

View our article on debugging test failures: https://codecrafters.io/debug

```

And here’s a snippet of my code:

```c
#include <stdio.h>
#include <readline/history.h>
#include <readline/readline.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

char *builtin_commands_array[] = {"echo", "type", "exit", "pwd"};
char *command_generator(const char *text, int state) {
  static int list_index;
  static int len;

  if (!state) {
    list_index = 0;
    len = strlen(text);
  }

  while (builtin_commands_array[list_index]) {
    const char *cmd = builtin_commands_array[list_index];
    list_index++;

    if (strncmp(cmd, text, len) == 0) {
      return strdup(cmd);
    }
  }

  return NULL;
}

char **command_completion(const char *text, int start, int end) {
  rl_attempted_completion_over = 1; // Prevent filename completion
  return rl_completion_matches(text, command_generator);
}

int main() {
  setbuf(stdout, NULL); // Flush after every printf

  char input[100];
  rl_attempted_completion_function = command_completion;
  while (1) {
    char *line = readline("\x1b[0;32m$\x1b[0m ");
    if(!line) break;
    
    if(strlen(line)==0) {
      free(line);
      continue;
    }
    add_history(line);
    strcpy(input, line);
    free(line);

    // Handle exit 0 command
    if (strcmp(input, "exit 0") == 0) {
      return 0;
    }

    // Handle echo command
    if (strncmp(input, "echo", 4) == 0) {
    //rest of my code

```

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [May 4, 2025, 8:34am UTC](https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906/3 "2025-05-04T08:34:33Z")

</div>

Hey @devnchill, could you [upload your code to GitHub](https://docs.codecrafters.io/features/publish-to-github) and share the link? It will be much easier to debug if I can run it directly.

---

<div class="post-metadata">

**Author:** ![devnchill](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/devnchill/32/7291_2.png) [@devnchill](https://forum.codecrafters.io/u/devnchill)\
**Post date:** [May 4, 2025, 1:26pm UTC](https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906/4 "2025-05-04T13:26:32Z")

</div>

> **[GitHub - devnchill/Cell: Shell built in c](https://github.com/devnchill/Cell)**
>
> Shell built in c

---

<div class="post-metadata">

**Author:** ![devnchill](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/devnchill/32/7291_2.png) [@devnchill](https://forum.codecrafters.io/u/devnchill)\
**Post date:** [May 5, 2025, 1:21pm UTC](https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906/6 "2025-05-05T13:21:34Z")

</div>

You there ?

---

<div class="post-metadata">

**Author:** ![devnchill](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/devnchill/32/7291_2.png) [@devnchill](https://forum.codecrafters.io/u/devnchill)\
**Post date:** [May 5, 2025, 1:22pm UTC](https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906/7 "2025-05-05T13:22:47Z")

</div>

@andy1li

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [May 5, 2025, 3:58pm UTC](https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906/8 "2025-05-05T15:58:42Z")

</div>

@devnchill You can add this line to `CMakeLists.txt`:

```auto
target_link_libraries(shell PRIVATE readline)

```

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/1/1889e0189bdb53f29b0f9e04b67b50b0fecb5c21.png)

---

<div class="post-metadata">

**Author:** ![devnchill](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/devnchill/32/7291_2.png) [@devnchill](https://forum.codecrafters.io/u/devnchill)\
**Post date:** [May 5, 2025, 5:58pm UTC](https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906/10 "2025-05-05T17:58:01Z")

</div>

![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/0/07709db2b6f61ad3453226682947decba8bba49b.png)  
It compiles now but test fails even though autocompetion works fine .

```sh
 CodeCraftersCell git:(master) codecrafters test  
Initiating test run...

⚡ This is a turbo test run. https://codecrafters.io/turbo

Running tests. Logs should appear shortly...

[compile] -- Running vcpkg install
[compile] All requested packages are currently installed.
[compile] Total install time: 67.5 us
[compile] -- Running vcpkg install - done
[compile] -- The C compiler identification is GNU 14.2.0
[compile] -- The CXX compiler identification is GNU 14.2.0
[compile] -- Detecting C compiler ABI info
[compile] -- Detecting C compiler ABI info - done
[compile] -- Check for working C compiler: /usr/bin/cc - skipped
[compile] -- Detecting C compile features
[compile] -- Detecting C compile features - done
[compile] -- Detecting CXX compiler ABI info
[compile] -- Detecting CXX compiler ABI info - done
[compile] -- Check for working CXX compiler: /usr/local/bin/c++ - skipped
[compile] -- Detecting CXX compile features
[compile] -- Detecting CXX compile features - done
[compile] -- Configuring done (1.3s)
[compile] -- Generating done (0.0s)
[compile] -- Build files have been written to: /app/build
[compile] [50%] Building C object CMakeFiles/shell.dir/src/main.c.o
[compile] [100%] Linking C executable shell
[compile] [100%] Built target shell
[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.

Debug = true

[tester::#QP2] Running tests for Stage #QP2 (Autocompletion - Builtin completion)
[tester::#QP2] Running ./your_program.sh
[tester::#QP2] ✓ Received prompt ($ )
[tester::#QP2] Typed "ech"
[tester::#QP2] ✓ Prompt line matches "$ ech"
[tester::#QP2] Pressed "<TAB>" (expecting autocomplete to "echo")
[your-program] $ ech
[tester::#QP2] Output does not match expected value.
[tester::#QP2] Expected: "$ echo "
[tester::#QP2] Received: "$ ech"
[tester::#QP2] Assertion failed.
[tester::#QP2] Test failed

View our article on debugging test failures: https://codecrafters.io/debug

```

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [May 6, 2025, 9:54pm UTC](https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906/12 "2025-05-06T21:54:47Z")

</div>

@devnchill I ran your code locally and it couldn’t auto-complete `echo`:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/0/0c37b47d0831dd0c8ed6d7ed56ce0056454cb524.png)

The issue seems to stem from this line:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/c/c943b2a71b932c02adc4bf2522e72572ad0389a3.png)

`list_index` can go out of bounds and cause an error.

---

<div class="post-metadata">

**Author:** ![devnchill](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/devnchill/32/7291_2.png) [@devnchill](https://forum.codecrafters.io/u/devnchill)\
**Post date:** [May 8, 2025, 1:47am UTC](https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906/14 "2025-05-08T01:47:24Z")

</div>

Oh . fixed it . It passes the tests now. BTW how are you able to run `you_program.sh` ? I can’t run that script .This is what I get .

```sh
 CodeCraftersCell git:(master) ✗ ./your_program.sh 
CMake Error at /usr/share/cmake/Modules/CMakeDetermineSystem.cmake:152 (message):
  Could not find toolchain file: /scripts/buildsystems/vcpkg.cmake
Call Stack (most recent call first):
  CMakeLists.txt:2 (project)

CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!

```

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [May 8, 2025, 2:45am UTC](https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906/15 "2025-05-08T02:45:54Z")

</div>

@devnchill Nice work! 🎉

To run `your_program.sh` locally, you’ll need to:

1. Install [vcpkg](https://vcpkg.io/).
2. Set the `VCPKG_ROOT` environment variable to the directory where `vcpkg` is installed.

Here’s how you can do that on Mac:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/5/567a7a7269f1c0fcb6ddcf9953423a95d27568d5.png)

---

<div class="post-metadata">

**Author:** ![devnchill](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/devnchill/32/7291_2.png) [@devnchill](https://forum.codecrafters.io/u/devnchill)\
**Post date:** [May 8, 2025, 4:01pm UTC](https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906/16 "2025-05-08T16:01:36Z")

</div>

thanks @andy1li that’s really helpful. appreaciate it.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [May 18, 2025, 8:35am UTC](https://forum.codecrafters.io/t/failed-to-compile-for-codecrafters/9906/17 "2025-05-18T08:35:15Z")

</div>

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