简体   繁体   English

如何使用 CMake 链接到 C 数学库?

[英]How to link to the C math library with CMake?

How do I add the math library to my CMake file?如何将math库添加到我的 CMake 文件中? This post references adding a target link library , yet I am not too familiar with C. An Additional post - Could someone please demonstrate an example. This post references adding a target link library ,但我不太熟悉 C。 附加帖子- 有人可以演示一个例子。 Documentation I am using C and I receive an undefined reference to 'pow' with the pow method of the math header.我正在使用 C 的文档,并且我收到了一个undefined reference to 'pow'的引用。

cmake_minimum_required(VERSION 3.3)
project(CSCI-E-28-Unix-Linux-Systems-Programming)

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

set(SOURCE_FILES
    CMakeLists.txt
    getchar.c
    main.cpp
        hw0
    more01.c)

#target_link_libraries(<math.h> m)

add_executable(main main.cpp)
add_executable(getchar getchar.c)
add_executable(more01 more01.c)
add_executable(argu print_all_arguments.c)
add_executable(chars chars.c)
add_executable(ch4 ch4.c)

How do I add the math library to my CMake file?如何将math库添加到我的CMake文件中? This post references adding a target link library , yet I am not too familiar with C. An Additional post - Could someone please demonstrate an example.这篇文章引用了添加目标链接库的内容,但是我对C不太熟悉。另一篇文章-有人可以演示一个例子。 Documentation I am using C and I receive an undefined reference to 'pow' with the pow method of the math header. 文档我正在使用C,并且使用数学标头的pow方法收到undefined reference to 'pow'

cmake_minimum_required(VERSION 3.3)
project(CSCI-E-28-Unix-Linux-Systems-Programming)

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

set(SOURCE_FILES
    CMakeLists.txt
    getchar.c
    main.cpp
        hw0
    more01.c)

#target_link_libraries(<math.h> m)

add_executable(main main.cpp)
add_executable(getchar getchar.c)
add_executable(more01 more01.c)
add_executable(argu print_all_arguments.c)
add_executable(chars chars.c)
add_executable(ch4 ch4.c)

How do I add the math library to my CMake file?如何将math库添加到我的CMake文件中? This post references adding a target link library , yet I am not too familiar with C. An Additional post - Could someone please demonstrate an example.这篇文章引用了添加目标链接库的内容,但是我对C不太熟悉。另一篇文章-有人可以演示一个例子。 Documentation I am using C and I receive an undefined reference to 'pow' with the pow method of the math header. 文档我正在使用C,并且使用数学标头的pow方法收到undefined reference to 'pow'

cmake_minimum_required(VERSION 3.3)
project(CSCI-E-28-Unix-Linux-Systems-Programming)

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

set(SOURCE_FILES
    CMakeLists.txt
    getchar.c
    main.cpp
        hw0
    more01.c)

#target_link_libraries(<math.h> m)

add_executable(main main.cpp)
add_executable(getchar getchar.c)
add_executable(more01 more01.c)
add_executable(argu print_all_arguments.c)
add_executable(chars chars.c)
add_executable(ch4 ch4.c)

I am frankly a bit surprised that this kind of question still doesn't have a proper answer for Modern CMake .坦率地说,我有点惊讶这种问题仍然没有Modern CMake的正确答案。 These days, the recommended (and portable) approach is this:这些天,推荐的(和可移植的)方法是这样的:

find_library(MATH_LIBRARY m)
if(MATH_LIBRARY)
    target_link_libraries(MyTarget PUBLIC ${MATH_LIBRARY})
endif()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM