Failed to compile for codecrafters

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:

  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:

#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

Hey @devnchill, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

You there ?

@andy1li

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

target_link_libraries(shell PRIVATE readline)


It compiles now but test fails even though autocompetion works fine .

  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

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

The issue seems to stem from this line:

list_index can go out of bounds and cause an error.

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 .

  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!
1 Like

@devnchill Nice work! :tada:

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

  1. Install vcpkg.
  2. Set the VCPKG_ROOT environment variable to the directory where vcpkg is installed.

Here’s how you can do that on Mac:

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

1 Like