Any linux programmers here?

Hey gang

I need to do a small [em]linux[/em] project. It is not something I do often. I was wondering if anyone could help me out with a simple example.

All I need are:

An example of a c++ shared object dynamic library (c++, using g++ to compile) that has 3 functions:
An init() function that takes a callback from the calling program,
a start() function that then calls the callback every 30 seconds,
and an end() function that stops this process.

and

A simple example app that just loads that library, calls the init function (passing a callback to a function within the example) and calls the start function.

That’s it. If anyone could point me to some usable tutorials or references to get that trivial example up and running, it would be really helpful. I have much more to do, but upon that base I can build the rest.

I know there are people here that could generate that in 5 minutes… anyone available?

Thanks

Okay then! Lol.

What about straight C?

How hard would it to be to write minimal linux .SO that calls a callback every 30 seconds in C?

Not too hard :slight_smile:

If you can wait another couple of weeks I can throw something together, just bogged down with other stuff till then. So sometime after the 18th.

@ mtylerjr - you did not specify your development environment. if you will be using eclipse with cdt, then this might help: Linux, C++ and other Tortures: Shared Libraries with Eclipse

Yes, eclipse (Neon) CDT and the gcc toolchain

Still no super linux gurus around to show me how this is done?

@ mtylerjr - I guess not really. Perhaps install mono and try it with c#?

the link I posted is not helpful?

1 Like

I just assumed that the instructions provided earlier had got the job done. I can certainly whip this up, but I (and I suspect a lot of others here) follow a philosophy of a) “showing how” is free b) “doing for” is not free … with OSS projects being the exception to rule (b).

Is this an OSS project or a paid gig?

there are also fun projects. :stuck_out_tongue:

I worked on a Linux project with shared libraries for five years. At the beginning of the project I found instructions similar to the referenced. Even with the instructions it was a pain. every few years we would need to go back to the instructions to add another library. consider the instructions a fishing lesson.

pain != fun … unless you are masochist :smiley:

Yeah - totally get that, and I am painfully aware that I am probably coming across as overly harsh. I love being part of and contributing to the community, and just to not be a butthead about the whole thing, I’ll try to gen up an example this weekend, time permitting.

<rabbit_trail>
@ mcalsyn - you bring up an interesting point about getting compensated for ones contribution. There are a lot of members who are very giving with their time and talent.
It would be interesting if GHI could setup a program that would allow other users to “gift” GHI products or gift cards to other users as a way of saying Thank You. If GHI handled it then personal information, like shipping addresses, wouldn’t have to be made public. Gifts could also be given anonymously.

Just thinking out loud. Sorry for the rabbit trail.

Carry on. . .
</rabbit_trail>

It’s a fine idea, but I don’t feel any need for anonymity, and I guess I’d like to see GHI spend their cycles building something other than a gift registry :).

With that said, there is a fine line here between being helpful/community-minded and directly contributing to someone else’s paying gig (not saying that’s what this is, because @ mtylerjr hasn’t said one way or the other). And within the paying-gig scenario, there is another distinction to be made between ‘help me do’ and ‘do for me’.

@ mcalsyn - you make a good point about how to do versus do… I like to make my responses to questions very short, with pointers to the necessary information to allow the OP to answer their own question.

don’t spoon feed people, give them the spoon…

@ mtylerjr -

Would this help you?

I got a moment to poke at it this afternoon. Here is a solution tested in Visual Studio with the VS for Linux Development extension, and Eclipse CDT. Both were running against Ubuntu 16.04. If the problem is the actual plumbing of setting up VS or Eclipse projects and the previously provided links weren’t enough, then sing out and I will drop some zips of the projects. Side note: Making it work in VS involves some tricks that are not documented anywhere I could find in the search engines. I will probably write up a blog on that. This code would also work in a VC++ project that created a dll for Windows.

Library header (mylib.h in the shared library project):

#ifndef mylib_h__
 #define mylib_h__

 #include <functional>
 #include <memory>
 #include <atomic>
 #include <thread>

class Worker : public std::enable_shared_from_this<Worker>
{
public:
	Worker();
	virtual ~Worker();

	void Start(int interval, std::function<void(void)> func);
	void Stop();
	bool IsRunning() const noexcept;

private:
	std::atomic<bool> _execute;
	std::thread _workerThread;
};

 #endif  // mylib_h__

Library Code (mylib.cpp in the shared library project) :

#include <stdio.h>
 #include "mylib.h"

Worker::Worker() :
	_execute(false)
{
}

Worker::~Worker()
{
	if (_execute.load(std::memory_order_acquire))
	{
		Stop();
	}
}

void Worker::Start(int interval, std::function<void(void)> func)
{
	if (_execute.load(std::memory_order_acquire)) 
	{
		Stop();
	}
	_execute.store(true, std::memory_order_release);

	auto pthis = shared_from_this();
	_workerThread = std::thread([pthis, interval, func]()
	{
		while (pthis->_execute.load(std::memory_order_acquire)) {
			func();
			std::this_thread::sleep_for(
				std::chrono::milliseconds(interval));
		}
	});
}

void Worker::Stop()
{
	_execute.store(false, std::memory_order_release);
	if (_workerThread.joinable())
		_workerThread.join();
}

bool Worker::IsRunning() const noexcept 
{
	return (_execute.load(std::memory_order_acquire) && _workerThread.joinable());
}

Client code (main.cpp in the main executable project) :

#include <cstdio>
 #include "mylib.h"

class CSomeClient
{
public:
	void Initialize()
	{
		_worker = std::make_shared<Worker>();
		_worker->Start(2000, std::bind(&CSomeClient::HandleCallback, this));
	}

	void Shutdown()
	{
		_worker->Stop();
	}

	void HandleCallback()
	{
		printf("ping\n");
	}

private:
	std::shared_ptr<Worker> _worker;
};

int main()
{
    printf("hello from LinuxApp!\n");

	auto foo = new CSomeClient();
	foo->Initialize();
	std::this_thread::sleep_for(std::chrono::minutes(2));
	foo->Shutdown();

	printf("goodbye\n\n");

	return 0;
}
1 Like

Example Eclipse workspace here : [url]https://dl.dropboxusercontent.com/u/24099742/shared-lib-example.tar.gz[/url]

This is one of the reasons I have left a number of the technical groups on LinkedIn. There was far too many, “give me the code for my project” requests and very little in the way of them trying first and then asking questions. Most requests were from students working on their projects and for the most part, Google would have given them the answer but they were too lazy to even do that.

I don’t mind helping, even for commercial type work if the information is small as I’ve received help before myself for similar type work. I draw the line at supplying full code or designs though.

2 Likes

It would be a pretty pathetic paying gig if this simple project was the project they were paying for, lol.

I just needed some examples of a linux shared library periodically calling back a client (as well as setting up Eclipse CDT in Ubuntu, which I havent done in awhile)

Everything I try to do in Eclipse seems to be fighting it. I am so used to Visual Studio.

Thanks for the help, guys.

I am really interested in these linux extensions for VS though. I had no idea they existed.