0

I'm fairly certain I'm doing something stupid.

But even so, I'm getting this compile error when compiling a very basic c++ script that connects to a node.js server using TCP. It crashes when there just the mention of a string being used -- anywhere. I don't mind using unsigned char arrays for communication, but the main c++ code I want to connect to the (local) server uses strings for other functions. Even when strings are not touching this part of the code it crashes in compilation (it compiles all files just fine, making you get your hopes up, and then says NOPE!). It seems like gcc doesn't recognize the string datatype on a very low architectural level or something like that. Maybe I'm using some deprecated method for TCP, maybe my mac is broken, but I'm not sure... Anyone seen this before?

compile error:

gcc   mainsocket.o   -o mainsocket
Undefined symbols for architecture x86_64:
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string<std::__1::nullptr_t>(char const*) in mainsocket.o
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
      _main in mainsocket.o
  "std::terminate()", referenced from:
      ___clang_call_terminate in mainsocket.o
  "___cxa_begin_catch", referenced from:
      ___clang_call_terminate in mainsocket.o
  "___gxx_personality_v0", referenced from:
      _main in mainsocket.o
      Dwarf Exception Unwind Info (__eh_frame) in mainsocket.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mainsocket] Error 1

barebones code:

#include <stdio.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <unistd.h> 
#include <string>
//include <string.h>

#define PORT 1337 

using namespace std;

int main(int argc, char const *argv[]) { 

    //errors when uncommented, compiles fine when commented (it doesn't even do anything)
    string testmessage = "hello";

    int sock = 0, valread; 
    struct sockaddr_in serv_addr; 
    char *handshake = "handshake"; 
    char buffer[1024] = {0}; 
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 
        printf("\n Socket creation error \n"); 
        return -1; 
    } 
    serv_addr.sin_family = AF_INET; 
    serv_addr.sin_port = htons(PORT); 

    // Convert IPv4 and IPv6 addresses from text to binary form 
    if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) { 
        printf("\nInvalid address/ Address not supported \n"); 
        return -1; 
    } 
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { 
        printf("\nConnection Failed \n"); 
        return -1; 
    } 
    send(sock , handshake , strlen(handshake) , 0 ); 
    printf("Handshake sent\n"); 
    valread = read( sock , buffer, 1024); 
    printf("%s\n",buffer ); 
    return 0; 
} 
  • 2
    Using `g++` (or `clang++`) instead of `gcc` (or `clang`) will make sure that you link with the C++ libraries. (You can do it by hand, but why bother when you don't have to?) – molbdnilo Apr 16 '20 at 12:12
  • 2
    Side note: that's far from a "barebones" example of the problem. You shouldn't need anything more than `#include ` and `int main() { std::string s;}` – molbdnilo Apr 16 '20 at 12:14
  • *facepalm* that fixed it, thanks! – TomOnderwater Apr 16 '20 at 12:21

0 Answers0