user8109
user8109

Reputation: 189

Convert makefile to cmakelist

I have a makefile which looks like this:

CC = gcc 
OPTFLAGS = -O3 -fno-strict-aliasing -D_GNU_SOURCE
COPTIONS = -DLINUX -D_FILE_OFFSET_BITS=64 -std=c99 -Wall\
       -Wno-unused-function -Wno-unused-label -Wno-unused-variable\
       -Wno-parentheses -Wsequence-point

#Standard Libraries
STDLIBS = -lm

CFLAGS = $(COPTIONS)  $(OPTFLAGS)
LIBS = $(STDLIBS)

SOURCE = sd.c getopt.c cmdline.c util.c
EXE = sd

default:
    $(CC) $(CFLAGS) $(SOURCE) $(LIBS) -o $(EXE)

I want to write the Cmakelist.txt for the above makefile. I tried following:

cmake_minimum_required(VERSION 3.9)
project(project1)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "-O3 -fno-strict-aliasing -D_GNU_SOURCE DLINUX -D_FILE_OFFSET_BITS=64 -std=c99 -Wall -Wno-unused-function -Wno-unused-label -Wno-unused-variable -Wno-parentheses -Wsequence-point -lm" )

add_executable(project1
    cmdline.c
    getopt.c
    sd.c
    util.c)

After that still, I am getting an error

undefined reference to `sqrt' 

because my cmakefile is not able to load standard libraries(-lm). Can somebody help me with cmakelist. What is wrong with my cmakelist ?

Upvotes: 2

Views: 2035

Answers (1)

Pablo
Pablo

Reputation: 13590

You have to use target_link_libraries like this:

target_link_libraries(project1 m)

and put it after add_executable call, as the first argument of target_link_libraries is a known target, project1 in your case.

See this

Also note that CMAKE_CXX_FLAGS is the variable for the compiler flags, -lm is linker flag, so doing -lm during compilation is useless.

Upvotes: 3

Related Questions