paullouisageneau / libdatachannel

C/C++ WebRTC network library featuring Data Channels, Media Transport, and WebSockets
https://libdatachannel.org/
Mozilla Public License 2.0
1.79k stars 361 forks source link

Implementation in Codeblocks C++ #1114

Closed carl0529 closed 8 months ago

carl0529 commented 8 months ago

I would like to use the library in Codeblocks. I had follow build instruction successfully, and added "/usr/local/include" to Project build options > Search directories > Compiler. The compiler can link to the library and get some declared variables. But I cannot even run the sample codes due to "undefined reference". Is there any additional set-up required for using in Codeblocks?

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

#include <rtc/rtc.hpp>
using namespace rtc;
#include "include/json.hpp"
using nlohmann::json;

int main(){
    rtc::Configuration config;
    config.iceServers.emplace_back("stun:stun.l.google.com:19302");

    rtc::PeerConnection pc(config);
    pc.onLocalDescription([](rtc::Description sdp) {
        // Send the SDP to the remote peer
        cout << "send SDP: "<< std::string(sdp) << endl;
        // MY_SEND_DESCRIPTION_TO_REMOTE(std::string(sdp));
    });

    pc.onLocalCandidate([](rtc::Candidate candidate) {
        // Send the candidate to the remote peer
        cout << "send candidate: "
            << candidate.candidate() << ", "
            << candidate.mid()
            << endl;
        // MY_SEND_CANDIDATE_TO_REMOTE(candidate.candidate(), candidate.mid());
    });

    pc.onStateChange([](rtc::PeerConnection::State state) {
    std::cout << "State: " << state << std::endl; });

    pc.onGatheringStateChange([pc](rtc::PeerConnection::GatheringState state) {
        std::cout << "Gathering State: " << state << std::endl;
    });
    pc.setLocalDescription();

    return 0;
}
error
paullouisageneau commented 8 months ago

I would like to use the library in Codeblocks. I had follow build instruction successfully, and added "/usr/local/include" to Project build options > Search directories > Compiler. The compiler can link to the library and get some declared variables. But I cannot even run the sample codes due to "undefined reference". Is there any additional set-up required for using in Codeblocks?

There is a confusion here, finding the header allows the compiler to get the definitions but it is not sufficient to link the library itself. In addition to setting the include path for the compiler, you need to add the library you built to the linker (I guess in the "Linker" tab instead of "Compiler"). This is not specific to libdatachannel.

carl0529 commented 8 months ago

Yet, putting library path in Project build options > Search directories > Compiler is standard procedure. I have no problem setting-up in this way when using OpenCV libarary. In addition, it still fail to link even if I also add the path to Search directories > Linker.

Is there any flag required when using the library, such as -pthread, -lX11, pkg-config --flags --libs opencv4 placed after g++?

paullouisageneau commented 8 months ago

Is there any flag required when using the library, such as -pthread, -lX11, pkg-config --flags --libs opencv4 placed after g++?

Yes, that's what I meant by adding the library, you need to add datachannel (the g++ argument is -ldatachannel).

carl0529 commented 8 months ago

Thanks, it work now. I thought the flag was -libdatachannel and kept getting it wrong.