请求将给定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=c99in your Makefile. - Optimization Settings: All flags from
OPTFLAGS(-O3,-fno-strict-aliasing,-D_GNU_SOURCE) are included. - Compile Definitions:
-DLINUXand-D_FILE_OFFSET_BITS=64are added to match yourCOPTIONS. - Warning Rules: Every
-Walland-Wno-*flag is replicated exactly to keep the same warning behavior. - Source Files: The list of
.cfiles is identical to yourSOURCEvariable. - Linking: We link against the math library (
libm.so) usingtarget_link_libraries, which is the CMake equivalent of-lm.
How to use this:
- Save the code above as
CMakeLists.txtin the same folder as your source files. - Create a dedicated build directory (best practice for out-of-source builds):
mkdir build && cd build - Generate build files with CMake:
cmake .. - 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
相关产品推荐
相关产品推荐

