pocoproject / poco

The POCO C++ Libraries are powerful cross-platform C++ libraries for building network- and internet-based applications that run on desktop, server, mobile, IoT, and embedded systems.
https://pocoproject.org
Other
8.47k stars 2.18k forks source link

Minimum working example fails to link to inexistant PocoFoundationmt.lib #4794

Open ssooffiiaannee opened 2 days ago

ssooffiiaannee commented 2 days ago

Os : Windows 11 MSVC 2017

Goal: Make a minimum working example of a MongoDB client writing to a DB (make it compile at least, then make it work)

Code example :

#include <Poco/MongoDB/Connection.h>
#include <Poco/MongoDB/Database.h>
#include <Poco/MongoDB/Document.h>
#include <Poco/MongoDB/Array.h>
#include <Poco/MongoDB/QueryRequest.h>
#include <Poco/MongoDB/ResponseMessage.h>
#include <Poco/Exception.h>
#include <iostream>
#include <string>

int main() {
        Poco::MongoDB::Connection connection("localhost", 27017); 

        Poco::MongoDB::Database db("test_db"); 
        std::string collectionName = "test_collection";

        auto insertRequest = db.createInsertRequest(collectionName);
        Poco::MongoDB::Document *doc = new Poco::MongoDB::Document();
        doc->add("name", "John Doe");
        doc->add("age", 30);
        doc->add("email", "johndoe@example.com");
        insertRequest->documents().push_back(doc);

        connection.sendRequest(*insertRequest);
        std::cout << "Document inserted successfully!" << std::endl;

        Poco::MongoDB::QueryRequest *query = new Poco::MongoDB::QueryRequest(collectionName);
        query->selector().add("name", "John Doe");  // Search for documents where 'name' is 'John Doe'

        Poco::MongoDB::ResponseMessage response;
        connection.sendRequest(*query, response);

    return 0;
}

Command line used : I have cloned the project to : D:\poco I have built the project in D:\poco\cmake-build folder I did not install, I'd rather link to the build folder

"C:\path\to\mvs\2017\Community\Common7\Tools\VsDevCmd.bat" "C:\path\to\mvs\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx86\x86\cl.exe" /std:c++17 /EHsc \mongodb_testing.cpp /I"D:\poco\MongoDB\include" /I"D:\poco\Net\include" /I"D:\poco\Foundation\include" /link "D:\poco\cmake-build\lib\PocoFoundation.lib"

Error:

mongodb_testing.obj LINK : fatal error LNK1104: cannot open file 'PocoFoundationmt.lib'

I can't find the PocoFoundationmt lib after the build, I don't know why it is trying to link to a non-existant library

matejk commented 1 day ago

Does installing and using CMake module from installation work?

ssooffiiaannee commented 1 day ago

what do you mean by "CMake module from installation " ? I used to following instructions

$ git clone -b main https://github.com/pocoproject/poco.git
$ cd poco
$ mkdir cmake-build
$ cd cmake-build
$ cmake ..
$ cmake --build . --config Release

I used cmake v3.30.3 The build went fine, no errors in sight, the libraries have been built, cmake-build\lib\PocoFoundation.lib and PocoMongoDB.lib are there. But somehow it is loooking for PocoFoundationmt.lib which cannot be found anywhere.

matejk commented 1 day ago

You wrote above that you did not install.

Install Poco to some directory:

cmake --install . --prefix "SOME DIR"

Then use CMake find_package(Poco) in your example instead of setting include path and linking directly.

That is the supported way and should work.

matejk commented 1 day ago

Another option for simple examples like yours is to define POCO_NO_AUTOMATIC_LIBS on the command line.

ssooffiiaannee commented 13 hours ago

Now that I managed to build Poco, and link it to my example code, the DB is not added after running the executable, no error reported, the server is running in localhost:default port, and I launched a nodejs app that writes into the db, and It worked, so the problem is not in the server side.

I am not sure what went wrong, I can't find any doc about this.

#include <Poco/MongoDB/Connection.h>
#include <Poco/MongoDB/Database.h>
#include <Poco/MongoDB/Document.h>
#include <Poco/MongoDB/Array.h>
#include <Poco/MongoDB/QueryRequest.h>
#include <Poco/MongoDB/ResponseMessage.h>
#include <iostream>
#include <string>

int main() {

        Poco::MongoDB::Connection connection("127.0.0.1", 27017);

        Poco::MongoDB::Database db("test_db");  
        std::string collectionName = "test_collection";

        auto insertRequest = db.createInsertRequest(collectionName);
        Poco::MongoDB::Document::Ptr doc = new Poco::MongoDB::Document();
        doc->add("name", "John Doe");
        doc->add("age", 30);
        doc->add("email", "something@gmail.com");
        insertRequest->documents().push_back(doc);

        connection.sendRequest(*insertRequest);
        std::cout << "Document inserted successfully!" << std::endl;
        return 0;
}

TCP traffic capture shows TCP handshakes and insertion request submitted. image

matejk commented 4 minutes ago

Use the function that captures the response and then print it. See unit tests.