You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求将给定Makefile转换为对应的CMakeLists.txt

Got it, let's convert your Makefile to a proper CMakeLists.txt that matches all your original build settings. Here's the complete implementation with explanations:

Convert Makefile to CMakeLists.txt

Here's the CMakeLists.txt that fully replicates the behavior of your provided Makefile:

# Minimum required CMake version (3.10 is a safe, widely-supported baseline)
cmake_minimum_required(VERSION 3.10)

# Define project name and language
project(sd C)

# Enforce C99 standard (matches `-std=c99` in your Makefile)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Add optimization flags (matches OPTFLAGS from Makefile)
add_compile_options(
    -O3
    -fno-strict-aliasing
    -D_GNU_SOURCE
)

# Add compile definitions and warning flags (matches COPTIONS)
add_compile_options(
    -DLINUX
    -D_FILE_OFFSET_BITS=64
    -Wall
    -Wno-unused-function
    -Wno-unused-label
    -Wno-unused-variable
    -Wno-parentheses
    -Wsequence-point
)

# List of source files (matches SOURCE variable)
set(SOURCES
    sd.c
    getopt.c
    cmdline.c
    util.c
)

# Build the executable (matches EXE variable)
add_executable(sd ${SOURCES})

# Link against the math library (matches STDLIBS = -lm)
target_link_libraries(sd PRIVATE m)

Key details matching your original Makefile:

  • C Standard: We explicitly enforce C99, just like -std=c99 in your Makefile.
  • Optimization Settings: All flags from OPTFLAGS (-O3, -fno-strict-aliasing, -D_GNU_SOURCE) are included.
  • Compile Definitions: -DLINUX and -D_FILE_OFFSET_BITS=64 are added to match your COPTIONS.
  • Warning Rules: Every -Wall and -Wno-* flag is replicated exactly to keep the same warning behavior.
  • Source Files: The list of .c files is identical to your SOURCE variable.
  • Linking: We link against the math library (libm.so) using target_link_libraries, which is the CMake equivalent of -lm.

How to use this:

  1. Save the code above as CMakeLists.txt in the same folder as your source files.
  2. Create a dedicated build directory (best practice for out-of-source builds):
    mkdir build && cd build
    
  3. Generate build files with CMake:
    cmake ..
    
  4. Build the executable:
    make
    

This will produce the sd executable exactly like your original Makefile, but with the added benefits of CMake (cross-platform support, better dependency management, etc.).

内容的提问来源于stack exchange,提问作者user8109

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:26:03