3

I got the following compile error even though I added the include path to openssl. I'm using vscode on a MAC. Can you tell me how to fix it?

Error

main.cpp:3:10: fatal error: 'openssl/crypto.h' file not found
#include <openssl/crypto.h>
         ^
1 error generated.

main.cpp

#include <iostream>

#include <openssl/crypto.h>

using namespace std;

int main()
{
    cout << "hoge" << endl;
}

.vscode/tasks.json

{
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "args": ["-std=c++14", "-O2", "-l", "boost_system", "-l", "boost_thread", "-o", "test", "-g",
        "main.cpp"
    ],
    "showOutput": "always"
}

.vscode/c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "/usr/include",
                "/usr/local/include",
                "/usr/local/opt/openssl/include"
            ],
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": "",
                "path": [
                    "/usr/include",
                    "/usr/local/include",
                    "/usr/local/opt/openssl/include"
                ]
            }
        }
    ]
}

.vscode/c_cpp_properties.json

$ which openssl
/usr/bin/openssl

$ ls  /usr/local/opt/openssl/include/openssl | grep crypto.h
crypto.h

Update 1

I found the similar issue but still did not find the solution for me yet.

Compiling C programs using libssl on OS X El Capitan?

How to use OpenSSL with GCC?

http://qiita.com/marumaru/items/ca801c957986302f6fe6


Update 2

I tried to compile using g++ but it did not work too. My mac is OS X El Capitan version10.11.6

$ g++ main.cpp -L/usr/local/opt/openssl/lib -lssl -lcrypto -o test
main.cpp:3:10: fatal error: 'openssl/crypto.h' file not found
#include <openssl/crypto.h>
         ^
1 error generated.

Update 3

Problem solved. I added -I and -L options.

g++ main.cpp -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto -o test
Vaiden
  • 15,728
  • 7
  • 61
  • 91
zono
  • 8,366
  • 21
  • 75
  • 113

1 Answers1

1

Another solution: On Mac (but this should work on other OSs as well), I first updated openssl:

brew upgrade openssl

Then setup the following env variables:

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

Which I got by simply trying:

brew info openssl

I still had to update tasks.json, but now the changes worked (clang for c):

{
    "tasks": [
        {
            "type": "shell",
            "label": "clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-I/usr/local/opt/openssl/include",
                "-L/usr/local/opt/openssl/lib",
                "-lssl",
                "-lcrypto"
            ],
            "options": {
                "cwd": "/usr/bin"
            }
        }
    ],
    "version": "2.0.0"
}
Vaiden
  • 15,728
  • 7
  • 61
  • 91