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

使用CMake构建依赖FFTW3的应用报错,求助依赖配置方案

问题:CMake构建依赖FFTW3的应用时出现链接错误

原本我用以下Makefile可以构建一个依赖FFTW3的简单应用:

# specify the compiler
CXX=g++

# specify any compile-time flags
CXXFLAGS=-Wall -g

# specify the build target
TARGET=app.out

# specify the FFTW3 library path
FFTW_DIR=/opt/ohpc/pub/libs/gnu7/openmpi/fftw/3.3.7

# specify the FFTW3 library flags
FFTW_LIB=-lfftw3

# specify any directories containing header files other than /usr/include
INCLUDES=-I$(FFTW_DIR)/include

# specify library paths other than /usr/lib
LFLAGS=-L$(FFTW_DIR)/lib

# define the C++ source files
SRCS=main.cpp

# define the C++ object files
OBJS=$(SRCS:.cpp=.o)

.PHONY: depend clean

all: $(TARGET)
    @echo Compilation has been completed successfully.

$(TARGET): $(OBJS)
    $(CXX) $(CXXFLAGS) $(INCLUDES) -o $(TARGET) $(OBJS) $(LFLAGS) $(FFTW_LIB)

.cpp.o:
    $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

clean:
    $(RM) *.o *~ $(TARGET)

depend: $(SRCS)
    makedepend $(INCLUDES) $^

现在我改用CMake构建,写了如下CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
set(CMAKE_CXX_STANDARD 17)
project(app)

add_executable(app.out
        main.cpp
        /opt/ohpc/pub/libs/gnu7/openmpi/fftw/3.3.7/include/fftw3.h)
target_link_libraries(app.out /opt/ohpc/pub/libs/gnu7/openmpi/fftw/3.3.7/lib/fftw3)
target_include_directories(app.out PRIVATE /opt/ohpc/pub/libs/gnu7/openmpi/fftw/3.3.7/include)

执行make时出现错误:

Consolidate compiler generated dependencies of target app.out
make[2]: *** No rule to make target '/opt/ohpc/pub/libs/gnu7/openmpi/fftw/3.3.7/lib/fftw3', needed by 'app.out'.  Stop.
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/app.out.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

我不知道怎么为app.out添加生成fftw3所需的规则,请求帮助。


解决方案

你的错误根源有两个:

  1. 错误地将头文件加入add_executable:该命令仅需传入源代码文件(.cpp/.c等),头文件无需手动列出——CMake会通过target_include_directories自动识别并处理依赖。
  2. target_link_libraries用法错误:你直接传入了库的路径,但CMake需要的是库名称(或完整库文件名,如libfftw3.so),或者先指定库路径再用库名链接。

修正后的CMakeLists.txt

方式一:手动指定库路径与名称

cmake_minimum_required(VERSION 3.15)
set(CMAKE_CXX_STANDARD 17)
project(app)

# 仅添加源代码文件
add_executable(app.out main.cpp)

# 指定FFTW3头文件目录
target_include_directories(app.out PRIVATE /opt/ohpc/pub/libs/gnu7/openmpi/fftw/3.3.7/include)

# 指定FFTW3库目录
link_directories(/opt/ohpc/pub/libs/gnu7/openmpi/fftw/3.3.7/lib)

# 链接FFTW3库(等效于Makefile中的-lfftw3)
target_link_libraries(app.out PRIVATE fftw3)

# 可选:添加原Makefile中的编译选项
target_compile_options(app.out PRIVATE -Wall -g)

方式二:用find_package自动查找(推荐)

如果FFTW3的安装路径符合CMake查找规则,或你设置了FFTW3_ROOT环境变量,可通过find_package自动定位库,灵活性更强:

cmake_minimum_required(VERSION 3.15)
set(CMAKE_CXX_STANDARD 17)
project(app)

# 手动指定FFTW3根目录(若CMake自动查找失败时用)
set(FFTW3_ROOT /opt/ohpc/pub/libs/gnu7/openmpi/fftw/3.3.7)

# 查找FFTW3库
find_package(FFTW3 REQUIRED)

# 添加可执行文件
add_executable(app.out main.cpp)

# 链接FFTW3库
target_link_libraries(app.out PRIVATE FFTW3::FFTW3)

# 可选:添加原Makefile中的编译选项
target_compile_options(app.out PRIVATE -Wall -g)

额外说明

  • PRIVATE关键字表示该依赖仅作用于当前目标,不会传递给其他依赖此目标的模块,适配你的单目标场景。
  • 若需要Debug模式,可添加set(CMAKE_BUILD_TYPE Debug),CMake会自动启用调试编译选项。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 16:08:19