2010-11-21 7 views
1

중첩 된 익명 블록 때문에 코드가 컴파일되지 않습니까?

왜 다음과 같은 코드가 작동하지 않습니다 (가속 C++에서 발췌) :

#include <iostream> 
#include <string> 

int main() { 
    { 
     const std::string s = "a string"; 
     std::cout << s << std::endl; 

     { 
      const std::string s = "another string"; 
      std::cout << s << std::endl; 
     } 
    } 
} 

은 내가 C++에 둥지 익명 블록에 허용되지 않는 건가요?

hello: In function `_start': 
(.text+0x0): multiple definition of 
`_start' 
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.text+0x0): 
first defined here 
hello:(.rodata+0x0): multiple 
definition of `_fp_hw' 
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.rodata+0x0): 
first defined here hello: In function 
`_fini': (.fini+0x0): multiple 
definition of `_fini' 
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.fini+0x0): 
first defined here 
hello:(.rodata+0x4): multiple 
definition of `_IO_stdin_used' 
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.rodata.cst4+0x0): 
first defined here hello: In function 
`__data_start': (.data+0x0): multiple 
definition of `__data_start' 
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.data+0x0): 
first defined here hello: In function 
`__data_start': (.data+0x4): multiple 
definition of `__dso_handle' 
/usr/lib/gcc/i486-linux-gnu/4.4.3/crtbegin.o:(.data+0x0): 
first defined here hello: In function 
`_init': (.init+0x0): multiple 
definition of `_init' 
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.init+0x0): 
first defined here /tmp/cchh83A6.o: In 
function `main': 
hello.cpp:(.text+0x0): multiple 
definition of `main' 
hello:(.text+0xb4): first defined here 
/usr/lib/gcc/i486-linux-gnu/4.4.3/crtend.o:(.dtors+0x0): 
multiple definition of `__DTOR_END__' 
hello:(.dtors+0x4): first defined here 
/usr/bin/ld: warning: Cannot create 
.eh_frame_hdr section, --eh-frame-hdr 
ignored. /usr/bin/ld: error in 
hello(.eh_frame); no .eh_frame_hdr 
table will be created. collect2: ld 
returned 1 exit status
+0

이것을 컴파일하고 링크하는 데 사용하는 명령은 무엇입니까? 또한 "익명 블록"을 제거하더라도 여전히 오류가 있습니까? –

+1

오류가 귀하의 코드가 아닌 귀하의 빌드 환경에있는 것 같습니다. IDE에 대한 정보를 게시하고 설정 (makefile?) 및 생성 된 빌드 명령 (게시 한 출력 바로 전에 콘솔에서 볼 수 있음)을 게시하십시오. – Kos

+0

이름이 지정된 (비 익명) 블록의 모습은 어떻습니까? :) – fredoverflow

답변

3

동일한 것을 정의하는 2 개 이상의 개체 파일 (또는 .a 파일)과 연결되는 것처럼 보입니다. 하나 또는 다른 링크를 사용하면 링커 문제가 해결됩니다.

+0

D' oh. 네 말이 맞아,이게 문제 야. 'g ++ -o hello hello.cpp' 대신'g ++ hello hello.cpp'를 썼습니다. – helpermethod

6

그 코드가 잘 작동 : 소스를 컴파일하려고 할 때, GCC는 나에게 (나는 어떤 의미가 없습니다) 다음과 같은 오류를 제공합니다. 여기를 참조하십시오 Your code compiled (환경에 문제가 있습니다)

관련 문제