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