我能够使用 MakeFile 及其内容构建一个简单的应用程序...
# 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)
当我现在尝试申请时,我得到:
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
我不知道如何添加规则来使 fftw3 被 app.out 需要。
以下 CMakeLists.txt 文件解决了该问题...
cmake_minimum_required(VERSION 3.15)
set(CMAKE_CXX_STANDARD 17)
project(app)
# specify the FFTW3 library path
set(FFTW_DIR /opt/ohpc/pub/libs/gnu7/openmpi/fftw/3.3.7)
# find the FFTW3 library
find_library(FFTW_LIB NAMES fftw3 PATHS ${FFTW_DIR}/lib NO_DEFAULT_PATH)
# specify any directories containing header files
include_directories(${FFTW_DIR}/include)
add_executable(app.out main.cpp)
# link the FFTW3 library
target_link_libraries(app.out ${FFTW_LIB})