How to use pthread library in DevC++? - windows-vista

I downloaded pthread package from pthread. What should I do now to use it in DevC++?

Download pthreads devpak Download
Install it in Dev C++
Create new Project in Dev C++
After that go to Project menu -> Project Option -> In that select "Parameter Tab"
Select "add Library or object" option
Select "libpthreadGC2.a" file from installation directory of Dev c++
It will be in LIB directory.
Press ok
Now test following sample code ready for running..
Sample Code :
#include <iostream>
#include <pthread.h>
using namespace std;
void * fun_thread1(void *data)
{
for(int i=0;i<100;i++)
{
cout<<endl<<"In Thread 1"<<endl;
}
}
void * fun_thread2(void *data)
{
for(int i=0;i<100;i++)
{
cout<<endl<<"In Thread 2"<<endl;
}
}
int main(int argc, char *argv[])
{
int status;
// creating thread objects
pthread_t thrd_1;
pthread_t thrd_2;
// create thread
pthread_create(&thrd_1,NULL,fun_thread1,(void *)0);
pthread_create(&thrd_2,NULL,fun_thread2,(void *)0);
pthread_join(thrd_1, (void **)&status);
pthread_join(thrd_2, (void **)&status);
system("PAUSE");
return EXIT_SUCCESS;
}

Related

LNK2019 on function that does exist in an included *.cpp file

For Visual Studio 2019 on Windows 10 in a new project I'm getting an unexpected LNK2019 error.
I have a test function calling a function in another project where I've included the necessary *.cpp file into the test project so I can access the internals of a DLL for testing.
test.cpp:
#include "dllsubmodule.h"
void TestCountPagesInSomething()
{
// ... setup ...
int nPages;
nPages = CountPagesInSomething(iCurrentPos, hashFoundPages, hashPageCounts, sFoundData);
}
int main(int argc, char **argv)
{
TestCountPagesInSomething();
}
dllsubmodule.h
#pragma once
int CountPagesInSomething(
int iCurrentPos,
std::map<int, ERPPageInfo>& hashFoundPages,
std::map<std::string, int>& hashPageCounts,
const wxString& sFoundData
);
dllsubmodule.cpp
#include "stdafx.h"
#include "dllsubmodule.h"
int CountPagesInSomething(
int iCurrentPos,
std::map<int, ERPPageInfo>& hashFoundPages,
std::map<std::string, int>& hashPageCounts,
const wxString& sFoundData
)
{
// implementation code here...
}
For whatever reason the linker error seems to be complaining that CountPagesInSomething() doesn't exist. I suspect I'm having trouble with name mangling variations but I've no idea how to diagnose the problem.
Note: CountPagesInSomething() is internal to the DLL and not a DLL export.
Right now this is just a console project. I'm trying to get a basic printf() test working before I figure out what kind of unit test framework to use.

ipref3 dll for windows

I try to build ipref3.dll for windows
I found How to compile iperf3 for Windows
Built it but i got only iperf3.exe and libiperf.a
I found, how create dll manual
gcc -s -shared -o iperf3.dll units.o timer.o tcp_window_size.o tcp_info.o net.o iperf_util.o iperf_sctp.o iperf_udp.o iperf_tcp.o iperf_server_api.o iperf_locale.o iperf_client_api.o iperf_error.o iperf_api.o cjson.o -Wl,--enable-auto-import,--export-all-symbols,--subsystem,windows
after i found how need to initialize
HMODULE h = LoadLibrary(TEXT("cygwin1.dll"));
PFN_CYGWIN_DLL_INIT init = (PFN_CYGWIN_DLL_INIT)GetProcAddress(h, "cygwin_dll_init");
init();
Now i can load dll and make initialization but when i start test iperf_run_client application is crashed
Unhandled exception at 0x611537C0 (cygwin1.dll) in iprerf-server.exe:
0xC0000005: Access violation reading location 0x00740000.
How can solve this problem?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <WinSock2.h>
//#include <unistd.h>
#include <string.h>
//#include <sysexits.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include "iperf_api.h"
#ifdef WIN64
#pragma comment(lib, "iperf3_64.lib")
#else
#pragma comment(lib, "iperf3.lib")
#endif
#pragma comment(lib, "ws2_32.lib")
typedef void *register_frame();
typedef int *hello_f();
typedef int(*PFN_HELLO)();
typedef void(*PFN_CYGWIN_DLL_INIT)();
#pragma pack(push, 1)
int main(int argc, char** argv)
{
WSADATA wsaData;
int wsaErr = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (wsaErr != 0) {
printf("WSAStartup failed with error: %d\n", wsaErr);
return 1;
}
//PFN_HELLO fnHello;
HMODULE /*hLib, */h = LoadLibrary(TEXT("cygwin1.dll"));
PFN_CYGWIN_DLL_INIT init = (PFN_CYGWIN_DLL_INIT)GetProcAddress(h, "cygwin_dll_init");
init();
char* argv0;
char* host;
int port;
struct iperf_test *test;
argv0 = strrchr(argv[0], '/');
if (argv0 != (char*)0)
++argv0;
else
argv0 = argv[0];
if (argc != 3) {
fprintf(stderr, "usage: %s [host] [port]\n", argv0);
exit(EXIT_FAILURE);
}
host = argv[1];
port = atoi(argv[2]);
test = iperf_new_test();
if (test == NULL) {
fprintf(stderr, "%s: failed to create test\n", argv0);
exit(EXIT_FAILURE);
}
iperf_defaults(test);
iperf_set_verbose(test, 1);
iperf_set_test_role(test, 'c');
iperf_set_test_server_hostname(test, host);
iperf_set_test_server_port(test, port);
/* iperf_set_test_reverse( test, 1 ); */
iperf_set_test_omit(test, 3);
iperf_set_test_duration(test, 5);
iperf_set_test_reporter_interval(test, 1);
iperf_set_test_stats_interval(test, 1);
/* iperf_set_test_json_output( test, 1 ); */
if (iperf_run_client(test) < 0) {
fprintf(stderr, "%s: error - %s\n", argv0, iperf_strerror(i_errno));
exit(EXIT_FAILURE);
}
if (iperf_get_test_json_output_string(test)) {
fprintf(iperf_get_test_outfile(test), "%zd bytes of JSON emitted\n",
strlen(iperf_get_test_json_output_string(test)));
}
iperf_free_test(test);
exit(EXIT_SUCCESS);
}
The reason why the shared lib is not built is:
libtool: warning: undefined symbols not allowed in x86_64-unknown-cygwin
shared libraries; building static only
the easy way to bypass it, in a clean build is to use:
$ make libiperf_la_LIBADD="-no-undefined"
The build will include the shared libray and the import library
$ find . -name "*dll*"
./src/.libs/cygiperf-0.dll
./src/.libs/libiperf.dll.a
For what I see to make a build on cygwin is also needed to remove a definition
in src/iperf_config.h after running configure
/* #define HAVE_SETPROCESSAFFINITYMASK 1 */
PS #1: iperf-2.0.5-1 is available as cygwin package
PS #2: your code is Windows-like while Cygwin is a Unix-like system, you can not mix them
I found solution
1) It need to create addition dll: my_crt0.dll
#include <sys/cygwin.h>
#include <stdlib.h>
typedef int (*MainFunc) (int argc, char *argv[], char **env);
void my_crt0 (MainFunc f)
{
cygwin_crt0(f);
}
gcc -c my_crt0.c
gcc -o my_crt0.dll my_crt0.o -s -shared -Wl,--subsystem,windows,--enable-auto-import,--export-all-symbols,--out-implib,my_crt0.lib
2) Modify main code
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <WinSock2.h>
#include <string.h>
#include "iperf_api.h"
#pragma comment(lib, "iperf3.lib")
#pragma comment(lib, "ws2_32.lib")
typedef int(*MainFunc) (int argc, char *argv[], char **env);
typedef void(*my_crt0)(MainFunc f);
int main2(int argc, char** argv, char **env)
{
char* argv0;
char* host;
int port;
struct iperf_test *test;
host = (char*)"127.0.0.1";
port = 4000;
test = iperf_new_test();
if (test == NULL) {
exit(EXIT_FAILURE);
}
iperf_defaults(test);
iperf_set_verbose(test, 1);
iperf_set_test_role(test, 'c');
iperf_set_test_server_hostname(test, host);
iperf_set_test_server_port(test, port);
/* iperf_set_test_reverse( test, 1 ); */
iperf_set_test_omit(test, 3);
iperf_set_test_duration(test, 5);
iperf_set_test_reporter_interval(test, 1);
iperf_set_test_stats_interval(test, 1);
/* iperf_set_test_json_output( test, 1 ); */
iperf_strerror(0);
if (iperf_run_client(test) < 0) {
fprintf(stderr, "%s: error - %s\n", argv0, iperf_strerror(i_errno));
exit(EXIT_FAILURE);
}
if (iperf_get_test_json_output_string(test)) {
fprintf(iperf_get_test_outfile(test), "%zd bytes of JSON emitted\n",
strlen(iperf_get_test_json_output_string(test)));
}
iperf_free_test(test);
exit(EXIT_SUCCESS);
return 1;
}
int main(int argc, char** argv)
{
WSADATA wsaData;
int wsaErr = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (wsaErr != 0) {
printf("WSAStartup failed with error: %d\n", wsaErr);
return 1;
}
{
HMODULE /*hLib, */h = LoadLibrary(TEXT("my_crt0.dll"));
my_crt0 init = (my_crt0)GetProcAddress(h, "my_crt0");
init(main2);
}
exit(EXIT_SUCCESS);
}
Now it compiled and worked to VS 2015

gtk glade need help

I am using glade to make an user interface.
i have successfully generated the glade file
Now i have to include this file in my C code.
I am using following code:
#include <stdlib.h>
#include<gtk/gtk.h>
int main (int argc, char *argv[])
{
GtkWidget *builder,*window,*button;
gtk_init (&argc, &argv);
builder=gtk_builder_new();
gtk_builder_add_from_file(builder,"shiv.glade",NULL);
window=GTK_WIDGET (gtk_builder_get_object(builder,"window1")) ;
button=GTK_WIDGET (gtk_builder_get_object(builder,"button1"));
g_object_unref(G_OBJECT(builder));
gtk_widget_show(button);
gtk_widget_show(window);
gtk_main ();
return 0;
}
My UI is a simple window having a button without any callback function.
I am getting following errors on execution
GTK-CRITICAL **: IA__gtk_widget_show assertion 'GTK_IS_WIDGET(widget)' failed
Change:
GtkWidget *builder,*window,*button;
with:
GtkWidget *window,*button;
GtkBuilder *builder;
this should fix.
Example:
#include <stdlib.h>
#include <gtk/gtk.h>
static void
close_window ( GtkWidget *widget, gpointer window)
{
printf("application close...\n");
gtk_widget_destroy((GtkWidget*)window);
}
int main (int argc, char *argv[])
{
GtkWidget *window, *button;
GtkBuilder *builder;
gtk_init (&argc, &argv);
builder=gtk_builder_new();
gtk_builder_add_from_file(builder,"a.glade",NULL);
window = GTK_WIDGET (gtk_builder_get_object(builder,"window1")) ;
button = GTK_WIDGET (gtk_builder_get_object(builder,"button1"));
g_signal_connect (G_OBJECT (button), "clicked",G_CALLBACK (close_window),window);
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), G_OBJECT(window));
g_object_unref(G_OBJECT(builder));
gtk_widget_show_all( window );
gtk_main ();
return 0;
}
From GTK3 reference manual:
GtkBuilder — Build an interface from an XML UI definition;
GtkWidget — Base class for all widgets

Usage of spoolFileName() of the Wt library

After I upload a file, I am trying to print out its spoolFileName() but although the application runs smoothly it seems as if the string of the name is empty. Any idea where it is wrong? (It's not the size of the file, it's less than 50k)
#include <Wt/WApplication>
#include <Wt/WFileUpload>
#include <Wt/WProgressBar>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>
#include <Wt/Http/Request>
#include <Wt/WString>
using namespace Wt;
class HelloApplication: public WApplication {
public:
HelloApplication(const WEnvironment& env);
private:
WPushButton *uploadButton;
Wt::WFileUpload *fu;
Wt::WString g;
void greet();
void fileUploaded();
};
HelloApplication::HelloApplication(const WEnvironment& env) :
WApplication(env) {
root()->addStyleClass("container");
setTitle("Hello world"); // application title
fu = new Wt::WFileUpload(root());
fu->setFileTextSize(50); // Set the maximum file size to 50 kB.
fu->setProgressBar(new Wt::WProgressBar());
fu->setMargin(10, Wt::Right);
// Provide a button to start uploading.
uploadButton = new Wt::WPushButton("Send", root());
uploadButton->setMargin(10, Wt::Left | Wt::Right);
// Upload when the button is clicked.
uploadButton->clicked().connect(this, &HelloApplication::greet);
}
void HelloApplication::greet() {
fu->upload();
uploadButton->disable();
fu->uploaded().connect(this, &HelloApplication::fileUploaded);
g = fu->spoolFileName();
}
void HelloApplication::fileUploaded(){ // application title
root()->addWidget(new WText(g.value()));
}
WApplication *createApplication(const WEnvironment& env) {
return new HelloApplication(env);
}
int main(int argc, char **argv) {
return WRun(argc, argv, &createApplication);
}
I think the filename for the spool file is only known after the file is uploaded. Move
g = fu->spoolFileName();
to HelloApplication::fileUploaded().

Qt - get all opened windows title

how can i check whether a specific window is open or not. I only got part of the window's name. i thinking of using EnumWindows() in QT console app but i get a few errors stating "main.obj:-1: error: unresolved external symbol imp__GetWindowTextW#12 referenced in function "int __stdcall EnumWindowsProc(struct HWND *,long)" (?EnumWindowsProc##YGHPAUHWND__##J#Z)"
Below is my sample code
BOOL CALLBACK EnumWindowsProc(HWND hWnd, long lParam) {
char buff[255];
if (IsWindowVisible(hWnd)) {
GetWindowText(hWnd, (LPWSTR) buff, 254);
}
return TRUE;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
EnumWindows(EnumWindowsProc, 0);
return 0;
}
That's a linker error rather than a compile error.
You have correctly included windows.h but you also need to add the import libraries to your linker options. All three Win32 functions in your sample code require you to link user32.lib.
EnumWindowsProc is not from Qt, it's a windows API function, you need to include windows.h
I did not use Qt, and the code can pass complie, but the output seems NOT right. Anyway, here's my code
#include <conio.h>
#include <iostream>
#include <windows.h>
using namespace std;
char buff[255];
BOOL CALLBACK EnumWindowsProc(HWND hWnd, long lParam)
{
if (IsWindowVisible(hWnd))
{
GetWindowText(hWnd, (LPWSTR) buff, 254);
}
return TRUE;
}
int main()
{
EnumWindows(EnumWindowsProc, 0);
for(int i = 0; i != 254; ++i)
cout << buff[i];
getch();
return 0;
}
You can use:
Application.OpenForms["FormName"]
To check if the form is open or not.

Resources