Jesse
Jesse

Reputation: 911

Cmake and readline library in c program

I am having some issues getting a cmake configuration to build a simple program that relies on the readline header files. As a disclaimer I am very new to cmake.

The simple c program I wrote is:

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

int main(int argc, char* argv[])
{
    char prompt[] = "user> ";
    char* lineinput;
    int i = 0;
    while(1)
    {
    lineinput = readline(prompt);
    add_history(lineinput);
    printf("[%i] %s\n",i, lineinput);
    free(lineinput);
    i++;
    }
    return 0;
}

I can compile it just fine with gcc repl.c -Wall -lreadline -o srepl.c. So I know I installed the readline-dev package just fine with apt-get. I delete my executable and make a build directory and cd to it. The project root directory tree now looks like

-> tree
.
|- build
|- CMakeLists.txt
|- repl.c

The CMakeLists.txt file I wrote and am trying to use is:

cmake_minimum_required(VERSION 2.8.12)
project(srepl)
set(CMAKE_VERBOSE_MAKEFILE on)
include_directories(/usr/include/readline)
file(GLOB SOURCES "./*.c")
set(CMAKE_C_FLAGS "-std=c99 -Wall -lreadline")
add_executable(srepl ${SOURCES})

In the build directory I run cmake and then make. I get the following output:

╰─⇀ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++
-- Check for working CXX compiler: /usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jesse/Public/simplerepl/build

╰─⇀ make
/usr/bin/cmake -H/home/jesse/Public/simplerepl -B/home/jesse/Public/simpl
erepl/build --check-build-system CMakeFiles/Makefile.cmake 0            
/usr/bin/cmake -E cmake_progress_start /home/jesse/Public/simplerepl/buil
d/CMakeFiles /home/jesse/Public/simplerepl/build/CMakeFiles/progress.mark
s                                                                       
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/jesse/Public/simplerepl/build'
make -f CMakeFiles/srepl.dir/build.make CMakeFiles/srepl.dir/depend
make[2]: Entering directory `/home/jesse/Public/simplerepl/build'
cd /home/jesse/Public/simplerepl/build && /usr/bin/cmake -E cmake_depends
 "Unix Makefiles" /home/jesse/Public/simplerepl /home/jesse/Public/simple
repl /home/jesse/Public/simplerepl/build /home/jesse/Public/simplerepl/bu
ild /home/jesse/Public/simplerepl/build/CMakeFiles/srepl.dir/DependInfo.c
make --color=                                                           
Scanning dependencies of target srepl
make[2]: Leaving directory `/home/jesse/Public/simplerepl/build'
make -f CMakeFiles/srepl.dir/build.make CMakeFiles/srepl.dir/build
make[2]: Entering directory `/home/jesse/Public/simplerepl/build'
/usr/bin/cmake -E cmake_progress_report /home/jesse/Public/simplerepl/bui
ld/CMakeFiles 1                                                         
[100%] Building C object CMakeFiles/srepl.dir/repl.c.o
/usr/bin/gcc   -std=c99 -Wall -lreadline -I/usr/include/readline    -o CM
akeFiles/srepl.dir/repl.c.o   -c /home/jesse/Public/simplerepl/repl.c   
Linking C executable srepl
/usr/bin/cmake -E cmake_link_script CMakeFiles/srepl.dir/link.txt --verbo
se=1                                                                    
/usr/bin/gcc  -std=c99 -Wall -lreadline    CMakeFiles/srepl.dir/repl.c.o 
 -o srepl -rdynamic                                                     
CMakeFiles/srepl.dir/repl.c.o: In function `main':
repl.c:(.text+0x3e): undefined reference to `readline'
repl.c:(.text+0x4e): undefined reference to `add_history'
collect2: error: ld returned 1 exit status
make[2]: *** [srepl] Error 1
make[2]: Leaving directory `/home/jesse/Public/simplerepl/build'
make[1]: *** [CMakeFiles/srepl.dir/all] Error 2
make[1]: Leaving directory `/home/jesse/Public/simplerepl/build'
make: *** [all] Error 2

So I see that it is not seeing where my readline header files are. I even included where the directory that it is in with the include_directories() function which I didn't have to do with the simple gcc call.

Any pointers here with explanations or relevant documentation sections would be helpful. I really want to learn how to bring cmake into my workflow.

I am on Xubuntu 14.04 also.

Thanks

Upvotes: 3

Views: 5555

Answers (1)

user1969104
user1969104

Reputation: 2420

You need to do target_link_libraries after the add_executable as below

cmake_minimum_required(VERSION 2.8.12)
project(srepl)
set(CMAKE_VERBOSE_MAKEFILE on)
include_directories(/usr/include/readline)
file(GLOB SOURCES "./*.c")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall")
add_executable(srepl ${SOURCES})
target_link_libraries(srepl readline)

Upvotes: 3

Related Questions