我已经解决了两周如何构建libcurl项目的问题。今天我终于成功了,我紧随其后http://curl.haxx.se/libcurl/c/Using-libcurl-with-SSH-support-in-Visual-Studio-2010.pdf但在我想将libcurl添加到我自己的项目中之后,它抛出了很多LNK错误。下面是我自己的项目代码,我想在其中添加libcurl。它只用于从url下载文件:
#include "stdafx.h"
#include "DlData.h"
#include "curl.h"
//#define CURL_STATICLIB
//#pragma comment(lib, "libcurld")
//#pragma comment(lib, "ws2_32")
//#pragma comment(lib, "Wldap32.lib")
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
};
int _tmain(int argc, _TCHAR* argv[])
{
DlData test;
test.PrintMsg("this is test!");
test.WaitForClick();
//Part for download file
CURL *curl;
ofstream fp;
CURLcode res;
char *url = "http://football-data.co.uk/mmz4281/1415/E0.csv";
char outfilename[FILENAME_MAX] = "C:\Users\zdenik\Documents\GitProjects\Predictor\Files\";
curl = curl_easy_init();
if (curl)
{
fp.open(outfilename);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
fp.close();
}
cin.get();
return 0;
}
我正确地设置了我的项目属性(C/C++-General、Preprocessor、Linker-General、Input),并希望将其构建为application-exe文件。到目前为止,我一切都很顺利,我能够创建libcurld.lib,并将其添加到项目属性中的链接器中。在我构建它之后,它抛出了很多LNK错误,看起来没有添加lib。输出如下:
1>------ Build started: Project: Predictor, Configuration: Debug Win32 ------
1> stdafx.cpp
1> Main.cpp
1> DlData.cpp
1> Generating Code...
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: ___iob_func already defined in LIBCMTD.lib(_file.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _calloc already defined in LIBCMTD.lib(dbgcalloc.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _free already defined in LIBCMTD.lib(dbgfree.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _malloc already defined in LIBCMTD.lib(dbgmalloc.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _realloc already defined in LIBCMTD.lib(dbgrealloc.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _memmove already defined in LIBCMTD.lib(memmove.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _strchr already defined in LIBCMTD.lib(strchr.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _strncmp already defined in LIBCMTD.lib(strncmp.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _strrchr already defined in LIBCMTD.lib(strrchr.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _fclose already defined in LIBCMTD.lib(fclose.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _fopen already defined in LIBCMTD.lib(fopen.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: __strtoi64 already defined in LIBCMTD.lib(strtoq.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: __errno already defined in LIBCMTD.lib(dosmap.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _tolower already defined in LIBCMTD.lib(tolower.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _fseek already defined in LIBCMTD.lib(fseek.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _ftell already defined in LIBCMTD.lib(ftell.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _isalpha already defined in LIBCMTD.lib(_ctype.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _isxdigit already defined in LIBCMTD.lib(_ctype.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _fwrite already defined in LIBCMTD.lib(fwrite.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _strtol already defined in LIBCMTD.lib(strtol.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _strtoul already defined in LIBCMTD.lib(strtol.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _isdigit already defined in LIBCMTD.lib(_ctype.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _fputc already defined in LIBCMTD.lib(fputc.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _sprintf already defined in LIBCMTD.lib(sprintf.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _isspace already defined in LIBCMTD.lib(_ctype.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _memchr already defined in LIBCMTD.lib(memchr.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _fflush already defined in LIBCMTD.lib(fflush.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _isalnum already defined in LIBCMTD.lib(_ctype.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _atoi already defined in LIBCMTD.lib(atox.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: __lseeki64 already defined in LIBCMTD.lib(lseeki64.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _getenv already defined in LIBCMTD.lib(getenv.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _isupper already defined in LIBCMTD.lib(_ctype.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _islower already defined in LIBCMTD.lib(_ctype.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _isprint already defined in LIBCMTD.lib(_ctype.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: _isgraph already defined in LIBCMTD.lib(_ctype.obj)
1>MSVCRTD.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)
1>MSVCRTD.lib(ti_inst.obj) : error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: __read already defined in LIBCMTD.lib(read.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: __write already defined in LIBCMTD.lib(write.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: __strnicmp already defined in LIBCMTD.lib(strnicmp.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: __close already defined in LIBCMTD.lib(close.obj)
1>MSVCRTD.lib(MSVCR110D.dll) : error LNK2005: __open already defined in LIBCMTD.lib(open.obj)
1>LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library
1>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>libcurld.lib(easy.obj) : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function _win32_init
1>libcurld.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__WSAStartup@8
1>libcurld.lib(easy.obj) : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function _win32_cleanup
1>libcurld.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__WSACleanup@0
1>libcurld.lib(misc.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurld.lib(openssl.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurld.lib(telnet.obj) : error LNK2019: unresolved external symbol __imp__WSAGetLastError@0 referenced in function _printsub
1>libcurld.lib(tftp.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurld.lib(ftp.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurld.lib(asyn-thread.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurld.lib(select.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurld.lib(sendf.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurld.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>libcurld.lib(global.obj) : error LNK2019: unresolved external symbol _OPENSSL_add_all_algorithms_noconf referenced in function _libssh2_init
1>libcurld.lib(openssl.obj) : error LNK2001: unresolved external symbol _OPENSSL_add_all_algorithms_noconf
...
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_load_error_strings referenced in function _Curl_ossl_init
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_SESSION_free referenced in function _Curl_ossl_session_free
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_set_session referenced in function _ossl_connect_step1
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_get_peer_certificate referenced in function _servercert
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_get_peer_cert_chain referenced in function _verifystatus
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_CTX_set_verify referenced in function _ossl_connect_step1
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_CTX_set_default_passwd_cb referenced in function _cert_stuff
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_CTX_set_default_passwd_cb_userdata referenced in function _cert_stuff
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_CTX_check_private_key referenced in function _cert_stuff
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_new referenced in function _cert_stuff
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_free referenced in function _Curl_ossl_close
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_connect referenced in function _ossl_connect_step2
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_read referenced in function _Curl_ossl_shutdown
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_peek referenced in function _Curl_ossl_check_cxn
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_write referenced in function _ossl_send
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_ctrl referenced in function _verifystatus
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_CTX_ctrl referenced in function _ossl_connect_step1
1>libcurld.lib(openssl.obj) : error LNK2019: unresolved external symbol _SSL_get_error referenced in function _Curl_ossl_shutdown
...
1>C:UserszdenikDocumentsGitProjectsPredictorDebugPredictor.exe : fatal error LNK1120: 213 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我的问题很简单。我的项目属性设置可能有什么问题,或者如何正确设置libcurld.lib?
我终于完成了。我下载了适用于Windows 32位的libcurl预编译版本,但在编译代码之前,我注意到char outfilename[FILENAME_MAX]="C:\Users\zdenik\Documents\GitProjects\Predictor\Files\"行中有错误。它需要在路径字符串的末尾添加文件名-C:\Users\zdenik\Documents\GitProjects\Predictor\Files\file.csv。例如,它不会导致编译错误,但应用程序在触发后会崩溃。
之后代码编译成功。