0

I have gcc installed:

$ gcc --version gcc (GCC) 8.2.1 20180831 Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I'm trying to compile this:

    #include <iostream>
    #include <openssl/sha.h>

    bool my_sha256(void* input, unsigned long length, unsigned char* md) {
        SHA256_CTX context;
        if (!SHA256_Init(&context)) {
            return false;
        }

        if (!SHA256_Update(&context, (unsigned char*)input, length)) {
            return false;
        }

        if (!SHA256_Final(md, &context)) {
            return false;
        }

        return true;
    }

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

        return 0;
    }

A compilation error:

  $ gcc main.cpp 
  /usr/bin/ld: /tmp/ccde3c1g.o: in function `my_sha256(void*, unsigned long, unsigned char*)':
  main.cpp:(.text+0x37): undefined reference to `SHA256_Init'
  /usr/bin/ld: main.cpp:(.text+0x64): undefined reference to `SHA256_Update'
  /usr/bin/ld: main.cpp:(.text+0x8a): undefined reference to `SHA256_Final'
  /usr/bin/ld: /tmp/ccde3c1g.o: in function `main':
  main.cpp:(.text+0xd2): undefined reference to `std::cout'
  /usr/bin/ld: main.cpp:(.text+0xd7): 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*)'
  /usr/bin/ld: /tmp/ccde3c1g.o: in function `__static_initialization_and_destruction_0(int, int)':
  main.cpp:(.text+0x107): undefined reference to `std::ios_base::Init::Init()'
  /usr/bin/ld: main.cpp:(.text+0x11c): undefined reference to `std::ios_base::Init::~Init()'
  collect2: error: ld returned 1 exit status

How to fix it?

Nattemando
  • 99
  • 1
  • 6
  • `g++ -lcrypto main.cpp` (assuming you have the OpenSSL libs in your lib path). You're missing the libcrypto.so reference, and you're missing linking the chosen C++ runtime because you're using gcc and not g++. – WhozCraig Oct 21 '18 at 18:42
  • @WhozCraig ok. in general should I use gcc or g++ for C++ apps? – Nattemando Oct 21 '18 at 19:10
  • @WhozCraig on linux it compiles, but on Mac not, the lib isn't getting found. But it's installed. How can I specify the full path to it? – Nattemando Oct 21 '18 at 19:14
  • openssl is no longer part of macos as of sierra. If you want to use it, you'll have to install or build it yourself. [Read this for more information](https://stackoverflow.com/questions/42044221/openssl-not-found-on-macos-sierra). – WhozCraig Oct 21 '18 at 20:44
  • @WhozCraig I've said - it's installed. How to use the full path? – Nattemando Oct 21 '18 at 22:10

0 Answers0