C++의 템플릿이 포함된 예제 컴파일에서...

이한길의 이미지

오랫만에 C++의 템플릿을 사용해볼라고 열심히 코딩했습니다.
그런데 컴파일이 제대로 되질 않더군요. 링크 부분에서..ㅜㅜ;

그래서 다음과 같은 간단한 예를 작성했습니다.

template <class DATA>
class Test
{
	public:
		Test(DATA x);
};

#include <iostream>
#include "tmp.h"

using namespace std;

template <class DATA>
Test<DATA>::Test(DATA x)
{
	cout<<x<<endl;
}

#include "tmp.h"
#include <iostream>

using namespace std;

main()
{
	Test<char> a('A');
}

이렇게 하고 처음껀 tmp.h, 다음껀 tmp.cpp... 그리고 마지막껀.. tm.cpp라고
이름을 붙이고 컴파일을 했습니다.

g++ tm.cpp tmp.cpp

그런데 에러가 납니다.

ccGVVvap.o(.text+0x2d):tm.cpp: undefined reference to Test<char>::Test[in-charge](char)'
collect2: ld returned 1 exit status

그러니깐 함수를 찾을 수 없단 말인데... (메소드라 해야 하나요?)
아무튼 이상합니다..

위의 것을 파일 하나에 묶어서

g++ tm.cpp

이렇게 하면 잘 컴파일도 되고 작동도 잘 하거든요..
왜그런걸까요?

myueho의 이미지

C++ template는 사용하는 type에 대해서만 코드를 만듭니다.

tmp.cpp에서는 정의된 template을 사용하지 않기 때문에 아무런 코드도 만들어 지지 않습니다.

만약 tmp.cpp에서 Test<char> tmp('A'); 와 같은 코드가 있었다면 Test<char>에 대한 코드가 생성될 것이고, 링크도 될겁니다.

inline 으로 쓰셔도 해결할 수 있을거 같습니다.

이한길의 이미지

그렇군요.. 그래서 아... 헤더에 써야 한다고 했던 건가요?
이를테면 위같은 경우...

template <class DATA> 
class Test 
{ 
   public: 
      Test(DATA x)
      {
         cout<<x<<endl;
      }
};

이런 식으로 말이지요...
휴~

----
먼저 알게 된 것을 알려주는 것은 즐거운 일이다!
http://hangulee.springnote.com
http://hangulee.egloos.com

asheap의 이미지

저도 해맸던 부분이로군요.

The C++ annotations

18.2.4: Template class member functions

의 내용이 도움이 되실 듯 합니다.

Quote:

The current Gnu g++ compiler does not support precompiled template classes, therefore the member functions of templates are inline functions.

라는 내용이 있고... 쭉 읽어보고 템플릿 사용에 많은 도움이 됐던 기억이 나는군요.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.