0

I started with C recently.
I need to develop a TS3 Plugin, which is controlled over a tcp socket. I would use a WebSocket.
I've found a bunch of libaries of Websockets but these are only for linux which uses pThread.h. And my TS3 Plugin will only run on Windows devices.

It is marked on the offical TS3 SDK Site that

Consisting of the ClientLib and the ServerLib, the TeamSpeak 3 SDK is delivered as shared libraries with a C style coding interface to provide developers direct access to the flexible and feature-rich set of TeamSpeak 3's core functions using simple API calls. With a standard C interface, the TeamSpeak SDK can be easily integrated into high-level programming languages. No matter what technology your application is using, the TeamSpeak SDK will fit in.

So that means that I could easly convert the ts3 api into C++ ?

Eric Goerens
  • 67
  • 11
  • 2
    Assuming it's well-written C, the C API *is* your C++ API. If you want classes and other stuff, you gotta do that yourself. –  Apr 22 '17 at 10:55
  • This post http://stackoverflow.com/questions/12066279/using-c-libraries-for-c-programs may help you. – Fryz Apr 22 '17 at 11:04

1 Answers1

2

The answer to your question, you don't need to convert the C libraries to C++. Basically, you can link the C library(Linux:.a/.so) or (Win:*.dll) and use that function into your corresponding modules.

Access C Code from Within C++ Source

  • In C++ language provides a "linkage specification" with which you declare that a function or object follows the program linkage conventions for a supported language.

  • The default linkage for objects and functions is C++. All C++ compilers also support C linkage, for some compatible C compiler.

  • When you need to access a function compiled with C linkage (for example, a function compiled by the C compiler, or a function written in assembler), declare the function to have C linkage.

Declaring Linkage Specifications

Use one of the following notations to declare that an object or function has the linkage of language language_name:

Example:

extern "C" void howdy(int);
extern "language_name" declaration ;
extern "language_name" { declaration ; declaration ; ... }
  • The first notation indicates that the declaration (or definition) that immediately follows has the linkage of language_name.
  • The second notation indicates that everything between the curly braces has the linkage of language_name, unless declared otherwise. Notice that you do not use a semicolon after the closing curly brace in the second notation.

You can nest linkage specifications, but the braces do not create scopes. Consider the following example:

extern "C" {
    void f();              // C linkage
    extern "C++" {
        void g();          // C++ linkage
        extern "C" void h(); // C linkage
        void g2();         // C++ linkage
    }
    extern "C++" void k(); // C++ linkage
    void m();              // C linkage
}

All the functions above are in the same global scope, despite the nested linkage specifiers.

Including C Headers in C++ Code

If you want to use a C library with its own defining header that was intended for C compilers, you can include the header in extern "C" brackets:

    extern "C" {
        #include "header.h"
    }

SIDENOTE: You need to to add this guard in your c header files in order to use that in C++.

#ifndef __YOURLIB_H_
#define __YOURLIB_H_

#ifdef __cplusplus
extern "C" {
#endif

int sample_func(int n);

#ifdef __cplusplus
}
#endif

#endif

Tutorial to use C lib in C++.

Community
  • 1
  • 1
danglingpointer
  • 4,708
  • 3
  • 24
  • 42