LoginSignup
0
0

More than 1 year has passed since last update.

Linux初心者 .soファイルとヘッダーファイルを使ってC++のソフトを作る

Last updated at Posted at 2023-04-28

Linux初心者が蹴躓くところですが、.soファイルとヘッダーファイルを使って、C++のソフトを作ってみた。(備忘録)

my_project/
├── CMakeLists.txt
├── include
│   └── test.h
├── src
│   └── main.cpp
└── lib
    └── libtest.so

libtest.soには、

int add(int a, int b);

として、aとbの足し算の関数を用意。

main関数

#include <stdio.h>
#include "test.h"

int main() {
  int a = 3, b = 4;
  int c = add(a, b);
  printf("%d + %d = %d\n", a, b, c);
  return 0;
}

CMakeLists.txtは下記。

cmake_minimum_required(VERSION 3.10)
project(my_project)

# includeディレクトリをインクルードパスに追加する
include_directories(include)

# 共有ライブラリをリンクする
link_directories(lib)
link_libraries(test)

# 実行可能ファイルをビルドする
add_executable(main src/main.cpp)

# 実行可能ファイルにリンクするライブラリを指定する
target_link_libraries(main test)

LD_LIBRARY_PATHを設定する。
libフォルダに設定するには

export LD_LIBRARY_PATH=/path/to/lib:$LD_LIBRARY_PATH

とする。

コマンドで

mkdir build
cmake ..
make

とすると、mainという実行ファイルができるので、
コマンドでmainを実行すると、
3 + 4 = 7
という出力を得られる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0