作者:男人要走火_293 | 来源:互联网 | 2023-09-01 14:11
我遇到了从Makefile
文件执行另一个CMakeLists.txt
的情况。
这是我的root
目录结构。
.
├── client.cpp
├── CMakeLists.txt
├── proto_lib
│ ├── Makefile
│ └── math.proto
└── server.cpp
这是一个简单的grpc
测试代码,我在其中尝试使用proto
创建静态库来编译Makefile
文件。然后将该库链接到CMakefile
中的最终目标。 Makefile运行正常。 CMakefile在一个可执行文件上也可以正常工作。
这里是CMakefile
:
cmake_minimum_required(VERSION 3.5)
project(client-server)
message(STATUS "Compiling Network Function : ${PROJECT_NAME}")
set(CMAKE_CXX_STANDARD 11)
message(STATUS "Project Directory: ${PROJECT_SOURCE_DIR}")
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIB_FILE ${CMAKE_CURRENT_SOURCE_DIR}/proto_lib/libmylib.a)
add_custom_target(libmylib.a ALL
COMMAND make
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/proto_lib
COMMENT "grpc headers and libraries are generating..."
)
add_executable(server server.cpp)
set(CMAKE_CXX_flaGS "-std=c++11 -Wall -g")
set(BASIC_libraRIES "-lpthread")
set(CMAKE_LINK_WHAT_YOU_USE ON)
set(LIBDIR "/usr/local/lib")
find_library(proto_location NAMES protobuf PATHS ${LIBDIR})
find_library(grpc_location NAMES grpc++ PATHS ${LIBDIR})
find_library(grpc_reflection_location NAMES grpc++_reflection PATHS ${LIBDIR})
find_library(dl_location NAMES dl PATHS ${LIBDIR})
find_library(grpc_location2 NAMES grpc PATHS ${LIBDIR})
if(proto_location)
message(STATUS "protobuf library found at " ${proto_location})
endif()
if(grpc_location)
message(STATUS "grpc++ found at " ${grpc_location})
endif()
if(grpc_reflection_location)
message(STATUS "grpc_reflection found at " ${grpc_reflection_location})
endif()
if(dl_location)
message(STATUS "dl found at " ${dl_location})
endif()
if(grpc_location2)
message(STATUS "grpc found at " ${grpc_location2})
endif()
target_link_libraries(server ${proto_location}
${LIB_FILE}
${grpc_location}
${grpc_reflection_location}
${dl_location}
${grpc_location2}
${BASIC_libraRIES}
)
当我在CMake之后输入命令make时。在根目录中。
它从以下输出开始...
Scanning dependencies of target libmylib.a
[ 33%] grpc headers and libraries are generating...
[ 33%] Built target libmylib.a
etc.. etc..
并且成功。(内部Makefile
使用proto
和grpc
库创建头文件和静态对象。此后server.cpp
可以从{内部包含头文件{1}}文件夹和CMake构建成功)
但是当我在proto_lib/
的末尾添加一个可执行文件时,
CMakeLists.txt
,然后add_executable(client client.cpp)
target_link_libraries(client ${proto_location}
${LIB_FILE}
${grpc_location}
${grpc_reflection_location}
${dl_location}
${grpc_location2}
${BASIC_libraRIES}
)
cmake .
&&
不从内部Makefile构建开始。而是从make
构建开始。
client
由于在这种情况下不会首先执行Scanning dependencies of target client
[ 20%] Building CXX object CMakeFiles/client.dir/client.cpp.o
/home/deka/Academic/gemini/learning/client_server_async/advanced1/client.cpp:10:10: fatal error: proto_lib/math.grpc.pb.h: No such file or directory
#include "proto_lib/math.grpc.pb.h"
,因此不会生成proto_lib/Makefile
,这就是上面的错误所显示的。
我的问题是:为什么再添加一个可执行文件不会在一开始就触发内部math.grpc.pb.h
构建?
谢谢!