클래스 파일을 공유라이브러리로 만들면 안될까요?

dltkddyd의 이미지

클래스의 헤더와 본문의 파일들을 공유라이브러리로 만들었습니다. class.h와 class.cc 파일로요. 그리고 이 파일들을 다음과 같이 공유라이브러리로 만들었습니다.

gcc -fPIC -c class.cc

그리고 이 파일로 공유라이브러리를 만들었습니다.

gcc -shared -WI,-soname,libtotal.so.1 -o libtotal.so.1 class.o

그리고 libtotal.so.1 파일을 /lib 폴더에 복사하고 /lib 폴더로 들어가서 다음과 같이 심볼릭 링크를 걸었습니다.

ln -s libtotal.so.1 libtotal.so

그리고 원래의 작업폴더로 들어와서

-main.cc-
#include "class.h"
int main() {
  //statement
  return 0;
}

라는 식으로 공유라이브러리 헤더를 넣었습니다. 그리고 다음과 같이 최종적으로 빌드를 하면,

gcc -o main main.cc -ltotal

무수히 많은 undefined reference to~~ 라는 링크에러를 출력합니다. 위에 예로 든 것은 하나의 예로서 만든 것이고요 실제 만든 코드는 만 여줄 이상이라서 올릴 수 가 없습니다. 더 간단한 예를 봤을 때, 클래스 자체는 공유라이브러리로 만들 수 없는 건지요? 만들 수 있는 것이라면 공유라이브러리 제작 과정 중에서 무엇이 잘못 됐는지 궁금합니다. 코드 짜는 시간보다 컴파일 다루는 것이 더 쉬웠으면 하는 바램으로 아둔한 자가 어리석은 질문 몇 자 올림....

chanik의 이미지

비슷하게 따라 해보니 잘 됩니다.
클래스를 공유라이브러리로 만들어도 됩니다.

$ cat foo.h 
class foo
{
public:
    foo();
    ~foo();
};
 
$ cat foo.cc
#include <iostream>
#include "foo.h"
 
foo::foo()
{
        std::cout << "Hello!" << std::endl;
}
 
foo::~foo()
{
        std::cout << "Bye!" << std::endl;
}
 
$ cat main.cc
#include "foo.h"
 
int main(void)
{
    foo f;
 
    return 0;
}
 
$ g++ -fPIC -c foo.cc
$ g++ -shared -WI,-soname,libfoo.so.1 -o libfoo.so.1 foo.o
$ ln -s libfoo.so.1 libfoo.so
$ g++ -o main main.cc -L. -lfoo
$ LD_LIBRARY_PATH=. ./main
Hello!
Bye!
chanik의 이미지

그런데, 혹시라도 foo.cc가 아래와 같은 형태로 작성되면
공유라이브러리 만드는데까지는 별 오류가 안 생기지만
main을 빌드할 때는 링크오류가 생깁니다.

혹시 이 경우가 아니더라도,
이렇게 짧은 테스트케이스를 만들어 문제가 재현되게 해 보시기 바랍니다.
거기서 출발하시면 질문도 구체적으로 올릴 수 있고 추적도 쉬워질 것입니다.

#include <iostream>
 
class foo
{
public:
        foo()
        {
                std::cout << "Hello!" << std::endl;
        }
 
        ~foo()
        {
                std::cout << "Bye!" << std::endl;
        }
};

$ g++ main.cc -o main -L. -lfoo
/tmp/cc0qTbzS.o: In function `main':
main.cc:(.text+0xe): undefined reference to `foo::foo()'
main.cc:(.text+0x1c): undefined reference to `foo::~foo()'
collect2: ld returned 1 exit status
chanik의 이미지

실험을 해보니, 또 한 가지 가능성이 있군요.
빌드할 때 g++ 대신 gcc를 이용해보니, 역시 공유라이브러리까지는 만들어지는데
main을 링크할 때 undefined reference 오류가 쏟아지네요.

c++ 코드는 g++로 빌드해야 합니다.
저는 c++ 코드를 gcc로 빌드하면 공유라이브러리 만들때부터 오류가 생길 것이라고 여겼고
아마 질문글 올리실때 실수로 gcc라고 쓰신걸로 생각했는데,
혹시 이 문제일지도 모르겠습니다.

$ gcc -fPIC -c foo.cc
$ gcc -shared -WI,-soname,libfoo.so.1 -o libfoo.so.1 foo.o
$ ln -s libfoo.so.1 libfoo.so
$ gcc -o main main.cc -L. -lfoo
/tmp/ccI4Wpgi.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
./libfoo.so: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
./libfoo.so: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
./libfoo.so: undefined reference to `std::ios_base::Init::~Init()'
./libfoo.so: undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
./libfoo.so: undefined reference to `std::ios_base::Init::Init()'
./libfoo.so: undefined reference to `std::cout'
collect2: ld returned 1 exit status

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.