How to link properly in CMake

I’m having some problems linking my program to the libssh header files.

I’m new to C Programming and I’m trying to do some things with the libssh library right now while also starting with a book on CMake.

When I compile I get the following errors:


    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

This is my 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})

And this is the main.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")] = '';
    } else if (strcmp(token, "USERNAME") == 0) {
      token = strtok(NULL, "=");
      strcpy(credentials->username, token);
      credentials->username[strcspn(credentials->username, "n")] = '';
    }
  }
  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;
}

When I search for libssh on my system:

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

I would appreciate it if someone could help me with this.

Thank you in advance!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật