错误:函数“rl_replace_line”的隐式声明在 C99 中无效 [-Werror,-Wimplicit-function-declaration]

问题描述 投票:0回答:3

我试图在我的代码中实现 rl_replace_line() 但当我尝试像这样编译它时:

gcc -lreadline test.c -o test

我收到此错误消息:

error: implicit declaration of function 'rl_replace_line' is invalid in C99 [-Werror,-Wimplicit-function-declaration]

但是我认为我使用了好的头文件? 这是我的代码:

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

char    *get_line()
{
    char *line;

    line = NULL;
    if (line)
    {
        free(line);
        line = NULL;
    }
    line = readline("Minishell>");
    if (line)
        add_history(line);
    return (line);
}

void    sig_handler(int signum)
{
    if (signum == SIGINT)
    {
        printf("\n");
        rl_on_new_line();
        rl_replace_line("", 0);
        rl_redisplay();
    }
}

int main(void)
{
    char    *line;

    signal(SIGINT, sig_handler);
    line = get_line();
    printf("%s\n", line);
}

不明白为什么不行,希望大家帮忙,谢谢!

c gcc compiler-errors readline
3个回答
2
投票

我设法通过包含正确的路径来解决我的问题:

-L .brew/opt/readline/lib
-I .brew/opt/readline/include

现在我像这样编译并且它正在工作:

gcc test.c -o test -lreadline -L .brew/opt/readline/lib -I .brew/opt/readline/include

1
投票

我通过使用命令 ==> find / -name libreadline.a 2> /dev/null 查找 libreadline.a 来解决该问题。

结果是==> /opt/homebrew/Cellar/readline/8.1.2/lib/libreadline.a

所以我设置了正确的编译路径: -L libs -lft -L /opt/homebrew/Cellar/readline/8.1.2/lib -lreadline。

不,这工作正常


0
投票

从brew info我们已经可以看到信息了:

$ brew info readline
==> readline: stable 8.2.13 (bottled) [keg-only]
Library for command-line editing
https://tiswww.case.edu/php/chet/readline/rltop.html
Installed
/opt/homebrew/Cellar/readline/8.2.13 (51 files, 1.7MB)
  Poured from bottle using the formulae.brew.sh API on 2024-11-27 at 18:39:10
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/r/readline.rb
License: GPL-3.0-or-later
==> Caveats
readline is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS provides BSD libedit.

For compilers to find readline you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/readline/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/readline/include"
==> Analytics

所以对于 macOS,我们可以在 CMake 中这样做:

if (APPLE)
  # Locate readline
  find_path(READLINE_INCLUDE_DIR readline/readline.h HINTS /opt/homebrew/opt/readline/include)
  find_library(READLINE_LIBRARY NAMES readline HINTS /opt/homebrew/opt/readline/lib)

  # Check if found
  if (NOT READLINE_INCLUDE_DIR OR NOT READLINE_LIBRARY)
    message(FATAL_ERROR "Readline library not found. Please install it with Homebrew.")
  endif()

  # Include the directory
  include_directories(${READLINE_INCLUDE_DIR})
  message(STATUS "readline include dir: ${READLINE_INCLUDE_DIR}")

  # Link the library
  target_link_libraries(client PRIVATE ${READLINE_LIBRARY})
else()
  target_link_libraries(client PRIVATE readline)
endif()
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.