2

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 ?

user8109
  • 189
  • 1
  • 12
  • Possible duplicate of [How to add "-l" (ell) compiler flag in CMake](https://stackoverflow.com/questions/43136418/how-to-add-l-ell-compiler-flag-in-cmake) – Tsyvarev Mar 21 '18 at 08:37

1 Answers1

3

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.

Pablo
  • 13,271
  • 4
  • 39
  • 59