Socket programming C++ error undefined reference [duplicate] - c++

This question already has answers here:
Visual C++ code not working in Code::Blocks
(2 answers)
Closed 8 years ago.
#pragma comment(lib,"Ws2_32.lib")
#include<sdkddkver.h>
#include<conio.h>
#include<stdio.h>
#include<iostream>
#include<WinSock2.h>
#include<windows.h>
#define SCK_VERSION2 0x0202
using namespace std;
int main()
{
long SUCCESSFUL;
WSAData WinSockData;
WORD DLLVERSION;
DLLVERSION=MAKEWORD(2,1);
SUCCESSFUL=WSAStartup(DLLVERSION,&WinSockData);
SOCKADDR_IN ADDRESS;
int AddressSize=sizeof(ADDRESS);
SOCKET sock_LISTEN;
SOCKET sock_CONNECTION;
sock_CONNECTION=socket(AF_INET,SOCK_STREAM,NULL);
ADDRESS.sin_addr.s_addr=inet_addr("127.0.0.1");
ADDRESS.sin_family=AF_INET;
ADDRESS.sin_port=htons(444);
sock_LISTEN=socket(AF_INET,SOCK_STREAM,NULL);
bind(sock_LISTEN,(SOCKADDR *)&ADDRESS,sizeof(ADDRESS));
listen(sock_LISTEN,SOMAXCONN);
for(;;)
{
cout<<"\n\tSERVER:Waiting for incoming connection...";
if(sock_CONNECTION=accept(sock_LISTEN,(SOCKADDR *)&ADDRESS,&AddressSize));
{
cout<<"\n\tA connection was found!"<<endl;
SUCCESSFUL=send(sock_CONNECTION,"Welcome! You are now connected to the Server!",46,NULL);
}
}
}
Whenever I try building this code for server I get following errors:
I'm totally new. I'm using code blocks. I've been searching for solution from last 4 days but I'm not understanding anything. Please help

If you are using CodeBlocks, then you use MinGW that goes with it (I guess, according to low experience level).
This feature works at Visual Studio compilers:
#pragma comment(lib,"Ws2_32.lib")
With MinGW such #pragma will not work.
Instead if it you have to open "Project" -> "Options", then choose your project at left tree (or "Debug"/"Release", if you want that change work just there), and then open tab "link settings".
Press "Add" and find library name like "libws2_32.a" in your MinGW/lib/ directory.
After try rebuild. Hope that will helps.

Related

iostream.h: no such file or directory [duplicate]

This question already has answers here:
fatal error: iostream.h no such file or directory [duplicate]
(3 answers)
Closed 6 years ago.
I am using Windows 8.1 and Dev C++ and I have the following code:
#include<iostream.h>
main()
{
cout<<"welcome to devc++";
system("pause");
}
The syntax is correct, but I get an error that says:
[Error] iostream.h: No such file or directory
I have tried to change to location of this .cpp folder, watched video tutorials, but I could not point out why I am getting this error and how to remove it.
You need to use #include<iostream> instead of #include<iostream.h>. The later one has been deprecated now; which is why you face the error. More details here.
Besides, main() being a function, should have a return type. So, you should write int main() and not just main().
Just do,
#include <iostream>
instead of
#include <iostream.h>
because, as C++ progressed from specific implementation to standard one,.h were deprecated from he library.
In addition to changing to
#include <iostream>
You can also add
using namespace std;
before main if you want to use cout without having to use std::cout.

undefined reference to `WinMain#16' with a main [duplicate]

This question already has answers here:
undefined reference to `WinMain#16'
(7 answers)
Closed 8 years ago.
I have a really simple shell of of a program. The editor i use is Scite and my compiler is MingW.
The answer to this is that I'm missing a main but i do have a main().
Main.cpp
#include <iostream>
#include "Money.h"
using namespace std;
int main()
{
}
Money.cpp
#include "Money.h"
#include <iostream>
using namespace std;
Money::Money()
{
cout << "test"
}
Money.h
#ifndef MONEY_H
#define MONEY_H
class Money
{
public:
Money();
private:
};
#endif //MONEY_H
Everytime I try to compile Money.cpp it gives me the error
libmingw32.a(main.o):(.text.startup+0xa7): undefined reference to `WinMain#16'
And I'm not sure what's wrong with the program. All the files are in the same directory. I'm fairly new to programming in C++ so if you can give me a very basic answer or fix it be greatly appreciated.
WinMain is the entry point of Windows "Win32" programs.
You are probably using Visual Studio wizard to create your C++ project, but you chose a Windows C++ application. Such an application is expected to have WinMain() as an entry point (the #16 part is name mangling decorations, according to Visual C++ compiler rules), but you haven't provided that in your code.
If you want to build a C++ console-mode application (with the classical standard main() entry point), you may want to choose the Win32 Console Application option when creating a new project with Visual Studio.
E.g. this is a screenshot from Visual Studio 2010:

Netbeans C++ Application won't build [duplicate]

This question already has an answer here:
How to add a library include path for NetBeans and gcc on Windows?
(1 answer)
Closed 8 years ago.
I recently installed the MiniGW compiler so that I can start learning C++. But when I go to create a new project and have it include main, I get two error messages from the start(after the project has been created) Cannot Find Include File <cstdlib> and Unable to resolve identifier std and when I try and 'Clean and Build' the project, the clean is successful and the build is not. I have done a google search and ran across this post Netbeans 7.2 shows "Unable to resolve identifier" , although build is successful , but I'm not sure my problem is exactly the same since my project won't build (or maybe it is, I don't know), and I'm also not sure I understand the accepted answer. Can this be multiple problems or just one? I want to get this fixed so I can learn!
/*
* File: main.cpp
* Author: Zf
*
* Created on December 16, 2014, 1:50 PM
*/
#include <cstdlib>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
return 0;
}
Edit: Lines 7 and 8 are where the error messages are.
Have you Tool Collection set properly ?
for example
<cstdlib> is found in C:\minGW\lib\gcc\mingw32\4.8.1\include\c++
Sounds like you need to tell the compiler where to find cstdlib. See this: How to add a library include path for NetBeans and gcc on Windows?
Cstdlib contains C functions, they are not wrapped in a namespace. Remove the using namespace std; line

why the error "undefined reference to `_imp__pthread_create" in IDE? [duplicate]

This question already has an answer here:
How do I fix undefined reference to _imp__*?
(1 answer)
Closed 9 years ago.
I got two error's undefined reference to _imp__pthread_create and undefined reference to _imp__pthread_join in Dev C++ in windows IDE, I am using dev c++ 5.4.1. Why this error? How can I resolve this? Could somebody shed some light? Thank you verymuch for your time!!
#include<iostream>
#include<pthread.h>
using namespace std;
void *uss_thread(void*){
cout<<"hello";
}
main()
{
pthread_t tid1;
pthread_create(&tid1, NULL, &uss_thread, NULL);
pthread_join(tid1,NULL);
}
Because you didn't link with lib pthread.
Download DevPack
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
SelectIt "libpthreadGC2.a" file from installation directory of Dev c++ : It will be in LIB directory.
Press Ok
Good Lucks !

C++ write and read from mysql database

I'm working on a project where we want to write to a mysql database, I've googled around and tried a few implementations but they all fail.
For example I tried this: http://markalexanderbain.suite101.com/using-a-mysql-databases-with-c-a70097
#include "StdAfx.h"
#include <iostream>
#include <my_global.h>
#include <mysql.h>
#pragma comment(lib, "libmysql")
#pragma comment(lib, "mysqlclient")
using namespace std;
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;
int main()
{mysql_init(&mysql);
//connection = mysql_real_connect(&mysql,"host","user","password","database",0,0,0);
connection = mysql_real_connect(&mysql,"localhost","bainm","not_telling","cpp_data",0,0,0);
if (connection == NULL)
cout << mysql_error(&mysql) << endl;
return 1;
}
It compiles and generates an exe file, but it closes every time I try to run it, I've added a cin.get at the end so it wouldn't close.
But the program still closes and Visual Studio 2010 gives me the following error message: The program '[32856] mysql test.exe: Native' has exited with code -1073741515 (0xc0000135).
The only thing that is common in all these implementations is the Include files and the #pragma comment(lib, "libmysql") and #pragma comment(lib, "mysqlclient")
I'm using these files which are included with mysql ga 5.5.16 32 bit.
So I'm looking for a way to write to a mysql database, if any one knows how to fix this to make it work or knows another way with a tutorial please let me know.
Error code 0xc0000135 means "The Application Failed To Initialize Properly". This is almost certainly due to a missing DLL (or two); probably the ones relating to MySQL mentioned in your #pragma lib statements.
Either add them to your path or copy them to the same directory as the .exe.
EDIT: Follow the instructions from here, in order to compile with the correct options. The file mysqlclient.dll should be included with the MySQL installation (do a system file find if you cannot find it).