' OpenSSL的/ crypto.h'在vscode上找不到文件

时间:2017-06-05 03:57:48

标签: c++ visual-studio-code

即使我添加了openssl的include路径,我也遇到了以下编译错误。我在MAC上使用vscode。你能告诉我如何解决它吗?

错误

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

更新1

我发现了类似的问题,但仍然没有为我找到解决方案。

Compiling C programs using libssl on OS X El Capitan?

How to use OpenSSL in GCC?

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

更新2

我尝试使用g ++进行编译,但它也没有用。我的mac是OS X El Capitan版本10.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.

更新3

问题解决了。我添加了-I和-L选项。

g++ main.cpp -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto -o test

1 个答案:

答案 0 :(得分:1)

另一种解决方案: 在Mac上(但这也应该在其他操作系统上也可以),我首先更新了openssl:

brew upgrade openssl

然后设置以下环境变量:

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

我只是尝试通过以下方法得到了

brew info openssl

我仍然必须更新task.json,但是现在所做的更改有效(c语言为clang):

{
    "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"
}