Kompilacja pliku do postaci biblioteki dynamicznej

0

Hej,
próbuje skompilować do postaci biblioteki dynamicznej.
Plik main.c

#include <stdio.h>
#include <stdlib.h>
#include <math_utils.h>
int main(){
    int x,y;
    scanf("%i%i", &x, &y);
    printf("%i\n%i", gcd(x, y), lcm(x, y));
}

Plik math_utils.h

#pragma once

int gcd(int x, int y);

int lcm(int x, int y);

Plik math_utils.c

#include "math_utils.h"

int gcd(int x, int y){
    int i;
    for(i = smaller(x,y); i>0; --i){
        if(x%i==0 && y%i==0){
            return i;
        }
    }
    return 0;
}

int lcm(int x, int y){
    int big = bigger(x, y);
    int small = smaller(x, y);
    int iteration = 1;
    while((big*iteration)%small!=0){
        ++iteration;
    }
    return big*iteration;
}

int smaller(int x, int y){
    if(x<y){
        return x;
    }
    return y;
}

int bigger(int x, int y){
    if(x>y){
        return x;
    }
    return y;
}

Polecenia którymi operuje w konsoli:

  1. gcc -fPIC -c math_utils.c

  2. gcc -shared -o libmath.dll math_utils.c

  3. gcc -o app main.c libmath.dll

    Problem ukazuje się po ostatnim poleceniu, a mianowice:
    main.c10: fatal error: math_utils.h: No such file or directory
    #include <math_utils.h>

Czy ktoś byłby mi w stanie odpowiedzieć na pytanie czemu mój program nie widzi math_utils.h?
Z góry dzięki za wszelaką pomoc

0

Ze studiów pamiętam, że #include "..." i #include <...> miało inne zachowanie - a masz obie składnie jednocześnie. Pewnie któreś jest nie teges.

1

nie podałeś ścieżki do nagłówka. Wpisz sobie w google man gcc i znajdź -I dir
Czytaj co pisze kompilator. Błąd mówi czego brakuje.

2

Polecam zainteresować się jakimś build manger (make, ninja, ...) lub build manager generator (cmake, bazel, ...).
cmake jest obecnie najbliższy do bycia standardem przemysłowym.

Te narzędzia działąją tak dobrze, że mało kto robi wszystko ręcznie z linii poleceń i trudno mi powiedzieć, co robisz źle.

gcc wkazuje, że użuwasz Linux/MacOS, a .dll, że używasz Windows (na Linux biblioteki mają rozszerzenie .so).

Najlepiej podaj pełna strukturę katalogu oraz skopuj zawartość konsoli.

Próbując dosłownie robić to co podałeś:

$ gcc -fPIC -c math functions.c
gcc: error: math: No such file or directory
gcc: error: functions.c: No such file or directory
gcc: fatal error: no input files
compilation terminated.

Nie podałeś źódeł dla: functions.c :/

2
marekR22:~/repo/4progSharedLib$ ls -la
total 20
drwxr-xr-x 2 marekR22 marekR22 4096 Apr 24 13:21 .
drwxr-xr-x 9 marekR22 marekR22 4096 Apr 24 13:05 ..
-rw-r--r-- 1 marekR22 marekR22  170 Apr 24 13:06 main.c
-rw-r--r-- 1 marekR22 marekR22  634 Apr 24 13:07 math_utils.c
-rw-r--r-- 1 marekR22 marekR22   62 Apr 24 13:07 math_utils.h
marekR22:~/repo/4progSharedLib$ gcc -fPIC -c math_utils.c
math_utils.c: In function ‘gcd’:
math_utils.c:5:18: warning: implicit declaration of function ‘smaller’ [-Wimplicit-function-declaration]
    5 |          for(i = smaller(x,y); i>0; --i){
      |                  ^~~~~~~
math_utils.c: In function ‘lcm’:
math_utils.c:14:16: warning: implicit declaration of function ‘bigger’ [-Wimplicit-function-declaration]
   14 |      int big = bigger(x, y);
      |                ^~~~~~
marekR22:~/repo/4progSharedLib$ gcc -shared -o libmath_utils.so math_utils.o
marekR22:~/repo/4progSharedLib$ gcc -o app main.c libmath_utils.so -I. -Wl,-rpath .
marekR22:~/repo/4progSharedLib$ ./app
1 2
1
2marekR2:~/repo/4progSharedLib$

Wesja z cmake:
CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(MathDynLib LANGUAGES C)

add_library(my_math SHARED math_utils.c math_utils.h)
target_include_directories(my_math PUBLIC ${CMAKE_SOURCE_DIR})

add_executable(app main.c)

target_link_libraries(app my_math)
marekR22:~/repo/4progSharedLib$ cmake -B build -S .
-- The C compiler identification is Clang 10.0.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/marekR22/repo/4progSharedLib/build
marekR22:~/repo/4progSharedLib$ cmake --build build
-- Configuring done
-- Generating done
-- Build files have been written to: /home/marekR22/repo/4progSharedLib/build
Scanning dependencies of target my_math
[ 25%] Building C object CMakeFiles/my_math.dir/math_utils.c.o
[ 50%] Linking C shared library libmy_math.so
[ 50%] Built target my_math
Scanning dependencies of target app
[ 75%] Building C object CMakeFiles/app.dir/main.c.o
[100%] Linking C executable app
[100%] Built target app
marekR22:~/repo/4progSharedLib$ cd build/
marekR22:~/repo/4progSharedLib/build$ ls
CMakeCache.txt  CMakeFiles  Makefile  app  cmake_install.cmake  libmy_math.so
marekR22:~/repo/4progSharedLib/build$ ./app
1 2
1
2marekR22:~/repo/4progSharedLib/build$
1

ja dla początkujących polecił bym przeklinanie przykładu z udziałem code+ cmake-tools
https://code.visualstudio.com/docs/cpp/cmake-linux

0

Nie zupełnie na temat pytania, zaś na temat kodu: podstawy matematyki się kłaniają:

#include <stdio.h>
#include <math.h>

int gcd(int a,int b) // Algorytm Euklidesa grubo ponad 2000 lat temu
{
	a=abs(a);
	b=abs(b);
	while(b)
	{
		int r=a%b;
		a=b;
		b=r;
	}
	return max(a,1);
}

int lcm(int a,int b)
{
	return abs(a)/gcd(a,b)*abs(b);
}

int main()
{
    int x,y;
    scanf("%i%i",&x,&y);
    printf("gcd: %i\nlcm: %i\n",gcd(x,y),lcm(x,y));
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1