Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link <math.h> library using CMake?

Tags:

cmake

clion

I included <math.h> library in my C source code. But I get compilation errors.

Error: 
**undefined reference to 'sqrt'
**undefined reference to 'atan'

How can I link to <math.h> in CMakeLists.txt?

like image 424
SEGV Avatar asked Oct 22 '16 19:10

SEGV


1 Answers

Cmakelists.txt file is like it:

cmake_minimum_required(VERSION 3.6)
project(project_name)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ")

set(SOURCE_FILES main.c)
add_executable(project_name ${SOURCE_FILES})

And you must add this command, for <math.h>

target_link_libraries(project_name PRIVATE m)

That's all.

like image 193
SEGV Avatar answered Sep 19 '22 19:09

SEGV