我正在尝试编译C++代码,但发生错误:



我正在使用freebsd,并且已经安装了lfcgi库。

我用来执行此操作的代码:



而我的代码:

#include "fcgio.h"

int main() {
    FCGX_Request request;

    FCGX_Init();

    FCGX_InitRequest(&request, 0, 0);

    while ( FCGX_Accept_r(&request) == 0 ) {
        fcgi_streambuf cout_fcgi_streambuf(request.out);

        std::cout.rdbuf(&cout_fcgi_streambuf);

        std::cout << "Content-type: text/html\r\n"
                     "\r\n"
                     "<h1>Hello world :)</h1>";
    }

    return 0;
}

谢谢!

----编辑----

我更改了命令进行编译



错误已更改:
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_streambuf<char, std::__1::char_traits<char> >::xsputn(char const*, long)'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::~basic_ostream()'
//usr/local/lib/libfcgi++.so: undefined reference to `virtual thunk to std::__1::basic_ostream<char, std::__1::char_traits<char> >::~basic_ostream()'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_ios<char, std::__1::char_traits<char> >::~basic_ios()'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_streambuf<char, std::__1::char_traits<char> >::~basic_streambuf()'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::~basic_ostream()'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_streambuf<char, std::__1::char_traits<char> >::imbue(std::__1::locale const&)'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_istream<char, std::__1::char_traits<char> >::~basic_istream()'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::ios_base::init(void*)'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_streambuf<char, std::__1::char_traits<char> >::showmanyc()'
//usr/local/lib/libfcgi++.so: undefined reference to `virtual thunk to std::__1::basic_istream<char, std::__1::char_traits<char> >::~basic_istream()'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_streambuf<char, std::__1::char_traits<char> >::seekpos(std::__1::fpos<__mbstate_t>, unsigned int)'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_streambuf<char, std::__1::char_traits<char> >::basic_streambuf()'
//usr/local/lib/libfcgi++.so: undefined reference to `typeinfo for std::__1::basic_streambuf<char, std::__1::char_traits<char> >'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_istream<char, std::__1::char_traits<char> >::~basic_istream()'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_istream<char, std::__1::char_traits<char> >::~basic_istream()'
//usr/local/lib/libfcgi++.so: undefined reference to `typeinfo for std::__1::basic_istream<char, std::__1::char_traits<char> >'
//usr/local/lib/libfcgi++.so: undefined reference to `virtual thunk to std::__1::basic_ostream<char, std::__1::char_traits<char> >::~basic_ostream()'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_streambuf<char, std::__1::char_traits<char> >::seekoff(long long, std::__1::ios_base::seekdir, unsigned int)'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_streambuf<char, std::__1::char_traits<char> >::xsgetn(char*, long)'
//usr/local/lib/libfcgi++.so: undefined reference to `typeinfo for std::__1::basic_ostream<char, std::__1::char_traits<char> >'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_streambuf<char, std::__1::char_traits<char> >::pbackfail(int)'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::~basic_ostream()'
//usr/local/lib/libfcgi++.so: undefined reference to `std::__1::basic_streambuf<char, std::__1::char_traits<char> >::setbuf(char*, long)'
//usr/local/lib/libfcgi++.so: undefined reference to `virtual thunk to std::__1::basic_istream<char, std::__1::char_traits<char> >::~basic_istream()'

最佳答案

函数FCGX_InitRequest是C库libfcgi中定义的C语言函数,不是
在C++库libfcgi++中。您需要同时链接两者,并且C++库取决于C库。所以
在链接中用-lfcgi++替换-lfcgi++ -lfcgi

08-19 15:58