我正在尝试使用 libssh 库做一些事情,同时也从一本关于 CMake 的书开始。
编译时出现以下错误:
intero@:~/Documents/projects/c/fastDeploy/build$ make
Consolidate compiler generated dependencies of target deployExe
[ 50%] Linking C executable deployExe/usr/bin/ld: CMakeFiles/deployExe.dir/main.c.o: in function `set_ssh_options':
main.c:(.text+0x2cc): undefined reference to `ssh_options_set'
/usr/bin/ld: main.c:(.text+0x2e5): undefined reference to `ssh_options_set'
/usr/bin/ld: main.c:(.text+0x2fe): undefined reference to `ssh_options_set'
/usr/bin/ld: CMakeFiles/deployExe.dir/main.c.o: in function `main':
main.c:(.text+0x359): undefined reference to `ssh_new'
/usr/bin/ld: main.c:(.text+0x3ab): undefined reference to `ssh_free'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/deployExe.dir/build.make:97: deployExe] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/deployExe.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
这是我的 CMakeList.txt:
cmake_minimum_required(VERSION 3.2)
project(fastDeploy)
add_executable(deployExe main.c)
find_package(LibSSH REQUIRED)
target_link_libraries(deployExe PRIVATE ${LIBSSH_LIBRARIES})
这是主要的。c:
#include <libssh/libssh.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ENV_PATH "../source/"
typedef struct {
char host[50];
char username[50];
int port;
int verbosity;
} ConnectionInfo;
void read_env(ConnectionInfo *credentials) {
char env_full_path[50];
int env_print =
snprintf(env_full_path, sizeof(env_full_path), "%s.env", ENV_PATH);
if (env_print < 0) {
printf("Error printing environment path to environment buffer.\n");
exit(1);
};
FILE *file = fopen(env_full_path, "r");
if (file == NULL) {
printf("Error opening env file.\n");
exit(1);
}
char line[50];
while (fgets(line, sizeof(line), file)) {
char *token = strtok(line, "=");
if (strcmp(token, "HOST") == 0) { // Fixed comparison
token = strtok(NULL, "=");
strcpy(credentials->host, token);
credentials->host[strcspn(credentials->host, "\n")] = '\0';
} else if (strcmp(token, "USERNAME") == 0) {
token = strtok(NULL, "=");
strcpy(credentials->username, token);
credentials->username[strcspn(credentials->username, "\n")] = '\0';
}
}
fclose(file);
credentials->verbosity = SSH_LOG_PROTOCOL;
credentials->port = 22;
}
// build folder, vps folder name, reminder that the build folder on vps will be
// replaced if it exists
void prompt_for_info(char *path_variable, char *vps_folder_name) {
puts("Please remember that the target folder on the vps will be deleted if "
"it already exists. If this is not desired, please cancel.");
puts("Enter build path:");
scanf("%s", path_variable);
puts("Enter vps folder name:");
scanf("%s", vps_folder_name);
}
void set_ssh_options(ssh_session session, ConnectionInfo *credentials) {
ssh_options_set(session, SSH_OPTIONS_HOST, &credentials->host);
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &credentials->verbosity);
ssh_options_set(session, SSH_OPTIONS_HOST, &credentials->port);
}
int main(int argc, char *argv[]) {
ConnectionInfo server_login;
read_env(&server_login);
char build_path[250], vps_folder_name[250];
prompt_for_info(build_path, vps_folder_name);
ssh_session ssh_conn = ssh_new();
if (ssh_conn == NULL) {
printf("Error creating SSH session.\n");
exit(-1);
}
set_ssh_options(ssh_conn, &server_login);
ssh_free(ssh_conn); // This should be used after using the session
return 0;
}
当我在系统上搜索 libssh 时:
intero@intero-MS-7971:~/Documents/projects/c/fastDeploy$ sudo find / -name 'libssh'
/usr/include/libssh
/usr/lib/x86_64-linux-gnu/cmake/libssh
find: ‘/tmp/.mount_nvimN16HgQ’: Permission denied
如果有人可以帮助我,我将不胜感激。
libssh安装提供了
libssh-config.cmake
配置脚本,它只定义了ssh
目标。正确使用 find_package()
结果是与该目标链接:
find_package(LIBSSH REQUIRED)
target_link_libraries(deployExe PRIVATE ssh)
该脚本未设置变量
LIBSSH_LIBRARIES
。您可以使用打印变量的值
message(STATUS "LIBSSH_LIBRARIES: ${LIBSSH_LIBRARIES}")
发现它是空的。