c network 프로그래밍에서 STL사용 어떻게 하나요?

webper81의 이미지

안녕하세요

전 c로 작성된 네트웍 프로그램을 사용하고 있습니다.

그런데 STL을 사용하려고 해서 컴파일 옵션에

gcc -o test test.c -lstdc++ -lpthread

라고 해보았지만 컴파일이 되질 않네요.

#include <iostream>
#include <vector>

이렇게 헤더파일만 넣어 보았는데 헤더파일을 찾을 수 없다고 나오네요.

그래서 test.cpp로 하고

g++ test test.cpp -lpthread 라고 하면

connection.cpp: In function `int listenServer (int)':
connection.cpp:66: `bzero' undeclared (first use this function)
connection.cpp:66: (Each undeclared identifier is reported only once
for each function it appears in.)
connection.cpp: In function `int acceptMainConnection (int)':
connection.cpp:122: cannot convert `int *' to `socklen_t *' for
argument `3' to `accept (int, sockaddr *, socklen_t *)'
connection.cpp: In function `int connect_srv (char *, int)':
connection.cpp:147: `inet_addr' undeclared (first use this function)
g++: Compilation of header file requested
make: *** [build] 오류 1

이렇게 나옵니다....

뭐가 잘못된 걸까요

STL에 관한 부분을 빼면 컴파일은 잘됩니다. 그런데 STL에 관한 부분만

넣으면 이러게 되고 마네요...

lsj0713의 이미지

gcc 대신 g++을 이용해 보세요.

buelgsk8er의 이미지

STL은 C에서는 사용할 수 없습니다. C++에서만 사용가능합니다.
그리고 단순히 확장자만 .c에서 .cpp로 바꾼다고 C코드가 C++코드로 되는건 아닙니다.. 하지만 그렇게 어려운 작업은 아닐테니 컴파일러가 뱉는 메시지를 하나하나 따라가면서 C에서는 허용되지만 C++에서 문제가 되는 부분들을 수정하시면 될 겁니다.

cdpark의 이미지

bzero는 <string.h>를 불러야 합니다. C++에서라면 <cstring> 함수겠죠?
inet_addr는 <sys/socket.h>, <netinet/in.h>, <arpa/inet.h>를 불러줘야 하고요.

라이브러리에 있는 함수를 쓸 때에 그에 필요한 헤더 파일을 꼬박꼬박 불러주는 습관을 들이는 게 정신건강(!)에 좋습니다. (math.h는 치명적이죠.)

B00m의 이미지

cdpark wrote:
라이브러리에 있는 함수를 쓸 때에 그에 필요한 헤더 파일을 꼬박꼬박 불러주는 습관을 들이는 게 정신건강(!)에 좋습니다. (math.h는 치명적이죠.)

그래서 저는 특별한 경우가 아니면 g++ 로 컴파일 합니다.

sandro의 이미지

gcc 3.0(맞나 ?) 이상은 표준 C 함수들을 호출 할때 std:: 를 붙여 줘야 합니다.

일일이 붙이기 귀찮으면 using namespace std;를 미리 선언 하면 될겁니다.

無心

댓글 달기

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