我对CMake有一个非常基本的问题,我将不胜感激。
问题:当我尝试使用纯GCC编译gSOAP时,一切都很好,但是当我使用CMake生成并生成Makefile时,出现连接错误。
这里是有效的gcc命令(但这不是一个好的解决方案):
c++ -o vms_server -Wall -fpermissive -DWITH_OPENSSL -DWITH_DOM -DWITH_ZLIB \
-I. -I gen -I libs/gsoap-2.8/gsoap/plugin -I libs/gsoap-2.8/gsoap/custom -I libs/gsoap-2.8/gsoap \
main.cpp \
gen/soapC.cpp \
gen/wsddClient.cpp \
gen/wsddServer.cpp \
gen/soapAdvancedSecurityServiceBindingProxy.cpp \
gen/soapDeviceBindingProxy.cpp \
gen/soapDeviceIOBindingProxy.cpp \
gen/soapImagingBindingProxy.cpp \
gen/soapMediaBindingProxy.cpp \
gen/soapPTZBindingProxy.cpp \
gen/soapPullPointSubscriptionBindingProxy.cpp \
gen/soapRemoteDiscoveryBindingProxy.cpp \
libs/gsoap-2.8/gsoap/stdsoap2.cpp \
libs/gsoap-2.8/gsoap/dom.cpp \
libs/gsoap-2.8/gsoap/plugin/smdevp.c \
libs/gsoap-2.8/gsoap/plugin/mecevp.c \
libs/gsoap-2.8/gsoap/plugin/wsaapi.c \
libs/gsoap-2.8/gsoap/plugin/wsseapi.c \
libs/gsoap-2.8/gsoap/plugin/wsddapi.c \
-lcrypto -lssl -lz
这里是有问题的CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.10.2)
project(vms_server LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_definitions(-Wall -fpermissive -DWITH_OPENSSL -DWITH_DOM -DWITH_ZLIB)
find_package(OpenSSL REQUIRED)
find_package(ZLIB)
SET(SRC_FILES main.cpp)
SET(GSOAP_DEP_FILES
libs/gsoap-2.8/gsoap/stdsoap2.cpp
libs/gsoap-2.8/gsoap/dom.cpp
libs/gsoap-2.8/gsoap/plugin/smdevp.c
libs/gsoap-2.8/gsoap/plugin/mecevp.c
libs/gsoap-2.8/gsoap/plugin/wsse2api.c
libs/gsoap-2.8/gsoap/plugin/wsaapi.c
libs/gsoap-2.8/gsoap/plugin/wsseapi.c
libs/gsoap-2.8/gsoap/plugin/wsddapi.c
)
SET(GEN_FILES
gen/soapC.cpp
gen/wsddClient.cpp
gen/wsddServer.cpp
gen/soapAdvancedSecurityServiceBindingProxy.cpp
gen/soapDeviceBindingProxy.cpp
gen/soapDeviceIOBindingProxy.cpp
gen/soapImagingBindingProxy.cpp
gen/soapMediaBindingProxy.cpp
gen/soapPTZBindingProxy.cpp
gen/soapPullPointSubscriptionBindingProxy.cpp
gen/soapRemoteDiscoveryBindingProxy.cpp
)
include_directories(
libs/gsoap-2.8/gsoap/plugin
libs/gsoap-2.8/gsoap/custom
libs/gsoap-2.8/gsoap
gen
)
add_executable(vms ${SRC_FILES} ${GSOAP_DEP_FILES} ${GEN_FILES})
target_link_libraries(vms OpenSSL::Crypto OpenSSL::SSL ${ZLIB_LIBRARIES})
cmake .
命令在没有警告或问题的情况下工作,但随后make
输出错误:
Scanning dependencies of target vms
[ 6%] Building CXX object CMakeFiles/vms.dir/main.cpp.o
/home/guy/Desktop/vms_server-c++/main.cpp: In function ‘int main()’:
/home/guy/Desktop/vms_server-c++/main.cpp:194:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<tt__Profile*>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
194 | for (int i = 0; i < GetProfilesResponse.Profiles.size(); ++i)
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/guy/Desktop/vms_server-c++/main.cpp: At global scope:
/home/guy/Desktop/vms_server-c++/main.cpp:308:13: warning: ‘void dyn_destroy_function(CRYPTO_dynlock_value*, const char*, int)’ defined but not used [-Wunused-function]
308 | static void dyn_destroy_function(struct CRYPTO_dynlock_value *l, const char *file, int line)
| ^~~~~~~~~~~~~~~~~~~~
/home/guy/Desktop/vms_server-c++/main.cpp:300:13: warning: ‘void dyn_lock_function(int, CRYPTO_dynlock_value*, const char*, int)’ defined but not used [-Wunused-function]
300 | static void dyn_lock_function(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line)
| ^~~~~~~~~~~~~~~~~
/home/guy/Desktop/vms_server-c++/main.cpp:291:37: warning: ‘CRYPTO_dynlock_value* dyn_create_function(const char*, int)’ defined but not used [-Wunused-function]
291 | static struct CRYPTO_dynlock_value *dyn_create_function(const char *file, int line)
| ^~~~~~~~~~~~~~~~~~~
[ 13%] Building CXX object CMakeFiles/vms.dir/libs/gsoap-2.8/gsoap/stdsoap2.cpp.o
[ 20%] Building CXX object CMakeFiles/vms.dir/libs/gsoap-2.8/gsoap/dom.cpp.o
[ 26%] Building CXX object CMakeFiles/vms.dir/gen/soapC.cpp.o
[ 33%] Building CXX object CMakeFiles/vms.dir/gen/wsddClient.cpp.o
[ 40%] Building CXX object CMakeFiles/vms.dir/gen/wsddServer.cpp.o
[ 46%] Building CXX object CMakeFiles/vms.dir/gen/soapAdvancedSecurityServiceBindingProxy.cpp.o
[ 53%] Building CXX object CMakeFiles/vms.dir/gen/soapDeviceBindingProxy.cpp.o
[ 60%] Building CXX object CMakeFiles/vms.dir/gen/soapDeviceIOBindingProxy.cpp.o
[ 66%] Building CXX object CMakeFiles/vms.dir/gen/soapImagingBindingProxy.cpp.o
[ 73%] Building CXX object CMakeFiles/vms.dir/gen/soapMediaBindingProxy.cpp.o
[ 80%] Building CXX object CMakeFiles/vms.dir/gen/soapPTZBindingProxy.cpp.o
[ 86%] Building CXX object CMakeFiles/vms.dir/gen/soapPullPointSubscriptionBindingProxy.cpp.o
[ 93%] Building CXX object CMakeFiles/vms.dir/gen/soapRemoteDiscoveryBindingProxy.cpp.o
[100%] Linking CXX executable vms
CMakeFiles/vms.dir/main.cpp.o: In function `set_credentials(soap*)':
main.cpp:(.text+0x65): undefined reference to `soap_wsse_delete_Security'
main.cpp:(.text+0x7d): undefined reference to `soap_wsse_add_Timestamp'
main.cpp:(.text+0xa2): undefined reference to `soap_wsse_add_UsernameTokenDigest'
CMakeFiles/vms.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x3ec): undefined reference to `soap_wsse'
CMakeFiles/vms.dir/gen/wsddServer.cpp.o: In function `soap_serve___wsdd__Hello(soap*)':
wsddServer.cpp:(.text+0x63c): undefined reference to `__wsdd__Hello(soap*, wsdd__HelloType*)'
CMakeFiles/vms.dir/gen/wsddServer.cpp.o: In function `soap_serve___wsdd__Bye(soap*)':
wsddServer.cpp:(.text+0x751): undefined reference to `__wsdd__Bye(soap*, wsdd__ByeType*)'
CMakeFiles/vms.dir/gen/wsddServer.cpp.o: In function `soap_serve___wsdd__Probe(soap*)':
wsddServer.cpp:(.text+0x866): undefined reference to `__wsdd__Probe(soap*, wsdd__ProbeType*)'
CMakeFiles/vms.dir/gen/wsddServer.cpp.o: In function `soap_serve___wsdd__ProbeMatches(soap*)':
wsddServer.cpp:(.text+0x97b): undefined reference to `__wsdd__ProbeMatches(soap*, wsdd__ProbeMatchesType*)'
CMakeFiles/vms.dir/gen/wsddServer.cpp.o: In function `soap_serve___wsdd__Resolve(soap*)':
wsddServer.cpp:(.text+0xa90): undefined reference to `__wsdd__Resolve(soap*, wsdd__ResolveType*)'
CMakeFiles/vms.dir/gen/wsddServer.cpp.o: In function `soap_serve___wsdd__ResolveMatches(soap*)':
wsddServer.cpp:(.text+0xba5): undefined reference to `__wsdd__ResolveMatches(soap*, wsdd__ResolveMatchesType*)'
collect2: error: ld returned 1 exit status
CMakeFiles/vms.dir/build.make:436: recipe for target 'vms' failed
make[2]: *** [vms] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/vms.dir/all' failed
make[1]: *** [CMakeFiles/vms.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
我看到当您使用CMake生成的Makefile进行编译时,并没有构建您的.c
文件;这是cause链接错误。因为您正在编译C ++项目(CXX
),所以CMake将忽略.c
文件。您可以尝试通过将.c
文件扩展名附加到c
列表,来告诉CMake将CMAKE_LANG_SOURCE_FILE_EXTENSIONS
视为C ++文件:
CMAKE_LANG_SOURCE_FILE_EXTENSIONS
这是一个巨大的举措,您可能不希望将此方法应用于所有C ++项目。更细粒度的方法是使用list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS c)
将每个LANGUAGE
源文件的.c
属性设置为C ++:
set_source_files_properties()