C and C++

Associate
Joined
15 Mar 2008
Posts
1,880
Trying to teach myself C++ and running into a few questions so I thought maybe some of you guys could help me out here :)

I come from a C# background so some of the syntax is rather familiar(obviously), in fact I think the biggest learning curve is learning what libraries I need to include for the purposes I need!

Anyway my question is, can I use a C header file in C++? More specifically could I simply include http://developer.spotify.com/en/libspotify/overview/ in a C++ project and it would just simply "work"? Or do I need some sort of wrapper for it?
 
Had some problems getting this to work in C++, but I put it down to my inexperience :p

So I tried getting it working in C thinking it would all slot together a bit smoother, however I now get compilation issues to do with things within their library.

This is harder than I thought it was going to be ;)
 
I only started using/learning c++ about a year and a half ago, coming from a 4 year C# background, so I kinda know the situation your in.

I would be happy to help if I can :)

Really appreciate the offer thanks :)

When I get home I'll post the code I've got so far, I think the main issue I have is with the function pointers. I believe they are the equivalent of delegates and events in .NET. The issue appears to be that they are not "firing" or at least If they are I cannot seem to get them to hit break points.

Anyway I think it will be all clearer once I post some code up tonight :p
 
Better late than never ;) and I'm still stumped.

Code:
#include <cstring>
extern "C" {
	#include <libspotify/api.h>
}
#include <stdint.h>
#include <iostream>
using namespace std;

const uint8_t g_appkey [] = {
/*api key*/
	};

const size_t g_appkey_size = sizeof(g_appkey);

static sp_session *session;

static void (SP_CALLCONV logged_in)(sp_session *session, sp_error error)
{
	if(error != SP_ERROR_OK)
	{
		cout << "Error on log in\r\n";
	}
	else
	{
		cout << "Logged in\r\n";
	}
}
static void (SP_CALLCONV logged_out)(sp_session* session)
{

}
static void (SP_CALLCONV connection_error)(sp_session *session, sp_error error)
{

}
static void (SP_CALLCONV notify_main_thread)(sp_session *sess)
{

}

static sp_session_callbacks sessionCallbacks = {
	&logged_in,
	&logged_out,
    NULL,
    &connection_error,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
};
static sp_session_config sessionConfig = {
		SPOTIFY_API_VERSION,
		"/tmp",
		"/tmp",
		g_appkey,
		g_appkey_size,
		"Test Client",
		&sessionCallbacks,
		NULL,
		TRUE,
		FALSE,
		TRUE
};

int main() {
	cout << "Hello World";
	sp_error err;

	err = sp_session_create(&sessionConfig, &session);

	if(err != SP_ERROR_OK )
	{
		return -1;
	}
	else
	{
		sp_session_login(session, /*username*/, /*password*/, 1);

		sp_session_logout(session);
	}
	string str;

	cin >> str;
	return 0;
}


Now if I put a break point on:
Code:
static void (SP_CALLCONV logged_in)(sp_session *session, sp_error error)

It never hits the break point which leads to me believe that it isn't hitting that block for whatever reason.
 
Thanks for the tip I'll give it a whirl!

Doing some more reading and the library isn't thread safe in parts, which I had not realised. I need to pay more attention :p

So the way I seemingly hook up to the function pointers is fine?
 
Back
Top Bottom