从 make 转换为 cmake

发布于 2025-01-17 06:33:02 字数 8326 浏览 2 评论 0原文

我已经遵循了教程并查看了示例,但在 MacBook Pro 上转换为 cmake 后,我仍然无法使用 make 来编译构建工作存储库。 cmake 文件中注释掉的代码结合了引用其他堆栈交换注释的努力。

Makefile

# tool macros
CC := g++
# TODO: Get rid of -Wunused-command-line-argument warnings
CCFLAGS := -lcurl -std=c++11
DBGFLAGS := -g
CCOBJFLAGS := $(CCFLAGS) -c

# path macros
BIN_PATH := bin
OBJ_PATH := obj
SRC_PATH := src
DBG_PATH := debug

# compile macros
TARGET_NAME := light-thing
ifeq ($(OS),Windows_NT)
    TARGET_NAME := $(addsuffix .exe,$(TARGET_NAME))
endif
TARGET := $(BIN_PATH)/$(TARGET_NAME)
TARGET_DEBUG := $(DBG_PATH)/$(TARGET_NAME)

# src files & obj files
SRC := $(foreach x, $(SRC_PATH), $(wildcard $(addprefix $(x)/*,.c*)))
OBJ := $(addprefix $(OBJ_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))
OBJ_DEBUG := $(addprefix $(DBG_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))

# clean files list
DISTCLEAN_LIST := $(OBJ) \
                  $(OBJ_DEBUG)
CLEAN_LIST := $(TARGET) \
              $(TARGET_DEBUG) \
              $(DISTCLEAN_LIST)

# default rule
default: makedir all

# non-phony targets
$(TARGET): $(OBJ)
    $(CC) $(CCFLAGS) -o $@ $(OBJ)

$(OBJ_PATH)/%.o: $(SRC_PATH)/%.c*
    $(CC) $(CCOBJFLAGS) -o $@ $<

$(DBG_PATH)/%.o: $(SRC_PATH)/%.c*
    $(CC) $(CCOBJFLAGS) $(DBGFLAGS) -o $@ $<

$(TARGET_DEBUG): $(OBJ_DEBUG)
    $(CC) $(CCFLAGS) $(DBGFLAGS) $(OBJ_DEBUG) -o $@

# phony rules
.PHONY: makedir
makedir:
    @mkdir -p $(BIN_PATH) $(OBJ_PATH) $(DBG_PATH)

.PHONY: all
all: $(TARGET)

.PHONY: debug
debug: $(TARGET_DEBUG)

.PHONY: clean
clean:
    @echo CLEAN $(CLEAN_LIST)
    @rm -f $(CLEAN_LIST)

.PHONY: distclean
distclean:
    @echo CLEAN $(CLEAN_LIST)
    @rm -f $(DISTCLEAN_LIST)

当前 CMakeLists

cmake_minimum_required(VERSION 3.8)
project(hue_console VERSION 1.0.0 LANGUAGES CXX)
option(huecon_BUILD_TESTS OFF)

set(CMAKE_BUILD_TYPE Debug)
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_FLAGS "-Wall -Wextra -fpermissive -Wno-unused-variable -Wno-unused-parameter -Wno-maybe-uninitialized -Wno-comment")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# set(CMAKE_CXX_STANDARD_REQUIRED ON)
# set(CMAKE_CXX_EXTENSIONS OFF)
# # Fix behavior of CMAKE_CXX_STANDARD when targeting macOS.
# if (POLICY CMP0025)
#   cmake_policy(SET CMP0025 NEW)
# endif ()

set(Boost_USE_STATIC_LIBS OFF) 
set(Boost_USE_MULTITHREADED ON)  
set(Boost_USE_STATIC_RUNTIME OFF) 
#find_package(Boost 1.45.0 COMPONENTS property_tree) 
find_package(Boost REQUIRED)
if(Boost_FOUND)
  include_directories(include)
  include_directories(${Boost_INCLUDE_DIRS}) 
  file(GLOB SOURCES "src/*.cpp")
  #target_link_libraries(progname ${Boost_LIBRARIES})
else(Boost_FOUND)
  message(FATAL_ERROR "Could not find the boost required.")
endif()

find_package(CURL)
set(requiredlibs)
if(CURL_FOUND)
  include_directories(${CURL_INCLUDE_DIRS})
  set(requiredlibs ${requiredlibs} ${CURL_LIBRARIES})
else(CURL_FOUND)
  message(FATAL_ERROR "Could not find the CURL library / dev files.")
endif(CURL_FOUND)

add_executable(hue_console 
  ${SOURCES})

cmake 输出

mreff555@Dans-MBP build % cmake ..
-- The CXX compiler identification is AppleClang 13.1.6.13160021
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefaul
t.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Boost: /usr/local/lib/cmake/Boost-1.78.0/BoostConfig.cmake (found version "1.78.0")
-- Found CURL: /usr/local/lib/libcurl.dylib (found version "7.79.1")
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mreff555/src/hue-api/build    

cmake ->制作输出

mreff555@Dans-MBP build % make
Consolidate compiler generated dependencies of target hue_console
[ 10%] Linking CXX executable hue_console
Undefined symbols for architecture x86_64:
  "_curl_easy_cleanup", referenced from:
      Command::get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command.cpp.o
      Command::post(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in comman
d.cpp.o
      Command::put(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command
.cpp.o
  "_curl_easy_init", referenced from:
      Command::get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command.cpp.o
      Command::post(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in comman
d.cpp.o
      Command::put(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command
.cpp.o
  "_curl_easy_perform", referenced from:
      Command::get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command.cpp.o
      Command::post(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in comman
d.cpp.o
      Command::put(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command
.cpp.o
  "_curl_easy_setopt", referenced from:
      Command::get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command.cpp.o
      Command::post(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in comman
d.cpp.o
      Command::put(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command
.cpp.o
  "_curl_easy_strerror", referenced from:
      Command::get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command.cpp.o
      Command::post(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in comman
d.cpp.o
  "_curl_global_cleanup", referenced from:
      Command::~Command() in command.cpp.o
  "_curl_global_init", referenced from:
      Command::Command(std::__1::shared_ptr<Config>) in command.cpp.o
  "_curl_slist_append", referenced from:
      Command::put(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command
.cpp.o
  "_curl_slist_free_all", referenced from:
      Command::put(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command
.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [hue_console] Error 1
make[1]: *** [CMakeFiles/hue_console.dir/all] Error 2
make: *** [all] Error 2

I've followed tutorials and looked at examples but I still can not get a build working repo with make to compile after converting to cmake on a MacBook Pro. The commented out code in the cmake file combines the efforts to reference other stack exchange comments.

Makefile

# tool macros
CC := g++
# TODO: Get rid of -Wunused-command-line-argument warnings
CCFLAGS := -lcurl -std=c++11
DBGFLAGS := -g
CCOBJFLAGS := $(CCFLAGS) -c

# path macros
BIN_PATH := bin
OBJ_PATH := obj
SRC_PATH := src
DBG_PATH := debug

# compile macros
TARGET_NAME := light-thing
ifeq ($(OS),Windows_NT)
    TARGET_NAME := $(addsuffix .exe,$(TARGET_NAME))
endif
TARGET := $(BIN_PATH)/$(TARGET_NAME)
TARGET_DEBUG := $(DBG_PATH)/$(TARGET_NAME)

# src files & obj files
SRC := $(foreach x, $(SRC_PATH), $(wildcard $(addprefix $(x)/*,.c*)))
OBJ := $(addprefix $(OBJ_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))
OBJ_DEBUG := $(addprefix $(DBG_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))

# clean files list
DISTCLEAN_LIST := $(OBJ) \
                  $(OBJ_DEBUG)
CLEAN_LIST := $(TARGET) \
              $(TARGET_DEBUG) \
              $(DISTCLEAN_LIST)

# default rule
default: makedir all

# non-phony targets
$(TARGET): $(OBJ)
    $(CC) $(CCFLAGS) -o $@ $(OBJ)

$(OBJ_PATH)/%.o: $(SRC_PATH)/%.c*
    $(CC) $(CCOBJFLAGS) -o $@ 
lt;

$(DBG_PATH)/%.o: $(SRC_PATH)/%.c*
    $(CC) $(CCOBJFLAGS) $(DBGFLAGS) -o $@ 
lt;

$(TARGET_DEBUG): $(OBJ_DEBUG)
    $(CC) $(CCFLAGS) $(DBGFLAGS) $(OBJ_DEBUG) -o $@

# phony rules
.PHONY: makedir
makedir:
    @mkdir -p $(BIN_PATH) $(OBJ_PATH) $(DBG_PATH)

.PHONY: all
all: $(TARGET)

.PHONY: debug
debug: $(TARGET_DEBUG)

.PHONY: clean
clean:
    @echo CLEAN $(CLEAN_LIST)
    @rm -f $(CLEAN_LIST)

.PHONY: distclean
distclean:
    @echo CLEAN $(CLEAN_LIST)
    @rm -f $(DISTCLEAN_LIST)

current CMakeLists

cmake_minimum_required(VERSION 3.8)
project(hue_console VERSION 1.0.0 LANGUAGES CXX)
option(huecon_BUILD_TESTS OFF)

set(CMAKE_BUILD_TYPE Debug)
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_FLAGS "-Wall -Wextra -fpermissive -Wno-unused-variable -Wno-unused-parameter -Wno-maybe-uninitialized -Wno-comment")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# set(CMAKE_CXX_STANDARD_REQUIRED ON)
# set(CMAKE_CXX_EXTENSIONS OFF)
# # Fix behavior of CMAKE_CXX_STANDARD when targeting macOS.
# if (POLICY CMP0025)
#   cmake_policy(SET CMP0025 NEW)
# endif ()

set(Boost_USE_STATIC_LIBS OFF) 
set(Boost_USE_MULTITHREADED ON)  
set(Boost_USE_STATIC_RUNTIME OFF) 
#find_package(Boost 1.45.0 COMPONENTS property_tree) 
find_package(Boost REQUIRED)
if(Boost_FOUND)
  include_directories(include)
  include_directories(${Boost_INCLUDE_DIRS}) 
  file(GLOB SOURCES "src/*.cpp")
  #target_link_libraries(progname ${Boost_LIBRARIES})
else(Boost_FOUND)
  message(FATAL_ERROR "Could not find the boost required.")
endif()

find_package(CURL)
set(requiredlibs)
if(CURL_FOUND)
  include_directories(${CURL_INCLUDE_DIRS})
  set(requiredlibs ${requiredlibs} ${CURL_LIBRARIES})
else(CURL_FOUND)
  message(FATAL_ERROR "Could not find the CURL library / dev files.")
endif(CURL_FOUND)

add_executable(hue_console 
  ${SOURCES})

cmake output

mreff555@Dans-MBP build % cmake ..
-- The CXX compiler identification is AppleClang 13.1.6.13160021
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefaul
t.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Boost: /usr/local/lib/cmake/Boost-1.78.0/BoostConfig.cmake (found version "1.78.0")
-- Found CURL: /usr/local/lib/libcurl.dylib (found version "7.79.1")
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mreff555/src/hue-api/build    

cmake -> make output

mreff555@Dans-MBP build % make
Consolidate compiler generated dependencies of target hue_console
[ 10%] Linking CXX executable hue_console
Undefined symbols for architecture x86_64:
  "_curl_easy_cleanup", referenced from:
      Command::get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command.cpp.o
      Command::post(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in comman
d.cpp.o
      Command::put(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command
.cpp.o
  "_curl_easy_init", referenced from:
      Command::get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command.cpp.o
      Command::post(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in comman
d.cpp.o
      Command::put(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command
.cpp.o
  "_curl_easy_perform", referenced from:
      Command::get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command.cpp.o
      Command::post(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in comman
d.cpp.o
      Command::put(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command
.cpp.o
  "_curl_easy_setopt", referenced from:
      Command::get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command.cpp.o
      Command::post(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in comman
d.cpp.o
      Command::put(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command
.cpp.o
  "_curl_easy_strerror", referenced from:
      Command::get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command.cpp.o
      Command::post(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in comman
d.cpp.o
  "_curl_global_cleanup", referenced from:
      Command::~Command() in command.cpp.o
  "_curl_global_init", referenced from:
      Command::Command(std::__1::shared_ptr<Config>) in command.cpp.o
  "_curl_slist_append", referenced from:
      Command::put(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command
.cpp.o
  "_curl_slist_free_all", referenced from:
      Command::put(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in command
.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [hue_console] Error 1
make[1]: *** [CMakeFiles/hue_console.dir/all] Error 2
make: *** [all] Error 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文