SFML 2.1 Networking - c++

I was trying to follow this tutorial, and I got this far.
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <iostream>
using namespace std;
int main(){
sf::IpAddress ip=sf::IpAddress::getLocalAddress();
sf::TcpSocket socket;
char connectiontype,mode;
char buffer[2000];
size_t received;
cout<<"s for server, c for client"<<endl;
cin>>connectiontype;
string text="connected to ";
if(connectiontype=='s'){
sf::TcpListener listener;
listener.listen(3000);
listener.accept(socket);
text+="server";
mode='s';
}
else if(connectiontype=='c'){
socket.connect(ip,3000);
text+="client";
mode='r';
}
socket.send(text.c_str(),text.length()+1);
socket.receive(buffer,sizeof(buffer),received);
cout<<buffer<<endl;
bool done=false;
while(!done){
if(mode=='s'){
getline(cin,text);
socket.send(text.c_str(),text.length()+1);
mode='r';
}
else if(mode=='r'){
socket.receive(buffer,sizeof(buffer),received);
if(received>0){
cout<<"received: "<<buffer<<endl;
mode='s';
}
}
}
return 0;
}
I compiled it, and got these errors:
I also tried adding sfml-network-2.dll and sfml-network-d-2.dll into my project folder, but it didn't work.
I'm also pretty sure that I set everything up correctly.
This is my setup:
I covered my name up, if you don't mind. Thanks!
Update:
I have reinstalled SFML, and I updated my code and my errors.

I solved my problem!
I changed each library to just the name, not where it is.
Example: Desktop/SFML-2.1/lib/libsfml-network-s-d.a->sfml-network

If you are pretty sure you have set up everything correctly, are you sure you added the SFML path to the Compiler include paths AND the SFML DLL to the Linker paths?
As you use Code::Blocks, have you setup your project "online game" exactly as described here?
http://www.sfml-dev.org/tutorials/2.1/start-cb.php
Update:
Have a look at this picture:
http://www.sfml-dev.org/tutorials/2.1/images/start-cb-link-libs.png
Are you sure, you've got sfml-network in the library list there? This might be the problem.
Adding the dlls to the project folder won't help, as it is a compiler/linker error and not a runtime library error.

You need to link to sfml-system too.
As a side note, you can find the official example about sockets here.

Related

Using Qt to configure OpenCV library failed

I'm new to Qt creator. Yesterday, I followed the official instructions to configure OpenCV library but it failed. I tried everything on the Internet but it just didn't work. Detailed are listed as belows:
test code is simple, I only want to ensure whether the library works:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello World!" << endl;
return 0;
}
My project configuration is like below :
I thought there might be a problem in debugger. I configure the debugger and I'm quite sure it's ok. The picture is here:
But it just doesn't work. When I click build and run, it says:
C1083: cannot open containing files: "opencv2/opencv.hpp": No such
file or directory.
What's strange is when I include <files> in the editor, the automatic code completion can detect the existence of the OpenCV library and hint after <opencv2/> that there are opencv.hpp, core.hpp .etc. and in the Include Hierarchy, the opencv.hpp exists.
So what might be the problem?

Code Blocks 16.01 can't find headers

code blocks 16.01 can't find headers.
I write some c++ code:
#include <iostream>
.....
int main()
{
...
}
it can be compiled without any errors or warnings, and run perfectly. But when I right-click the iostream then choose open iostream, it says that "Not found: iostream"
why? how to solve this problem?
I finally found that it's a matter of setting.
We should set the path of compiler as "...\codeblocks\MinGW\" rather than "...\codeblocks\MinGW\bin\". At meantime we should add an environment variable which is "...\codeblocks\MinGW\bin"
You're using int main function. In other words, it is waiting for a return of an integer. You can also solve this by just using void main function.

Undefined reference to 'SDL_main'

I've recently decided to try working with SDL with CodeBlocks 10.05. I started with the tutorial on http://www.sdltutorials.com/sdl-tutorial-basics and did my best to follow it. Unfortunately, I'm encountering:
..\..\..\..\..\..\SDL\SDL-1.2.15\lib\libSDLmain.a(SDL_win32_main.o):SDL_win32_main.c|| undefined reference to `SDL_main'|
when I try to compile it.
I've searched through many of the questions on this website and other tutorials (mainly the tutorial on LazyFoo and the CodeBlocks wiki) and can't seem to find a solution.
C:\SDL\SDL-1.2.15\include has been added in the Compiler tab (Search Directories)
C:\SDL\SDL-1.2.15\lib has been added in the Linker tab
The libraries libmingw32.a, libSDLmain.a, libSDL.dll.a are linked in that order
libmingw32.a from the MinGW\lib folder in the CodeBlocks installation directory
SDL.dll is in both the System32 folder and in the project folder
When attempting to follow the tutorial on the CodeBlocks wiki, I was told that SDL.h could not be found in the given directory (when making a new SDL project).
CApp.cpp
#include "CApp.h"
#include "SDL\SDL.h"
CApp::CApp(){
Surf_Display=NULL;
Running=true;
}
int CApp::OnExecute(){
if (OnInit()==false){
return -1;
}
SDL_Event Event;
while (Running){
while (SDL_PollEvent(&Event)){
OnEvent(&Event);
}
OnLoop();
OnRender();
}
OnCleanup();
return 0;
}
int main(int argc, char* argv[]){
CApp theApp;
return theApp.OnExecute();
}
CApp.h
#ifndef CAPP_H_INCLUDED
#define CAPP_H_INCLUDED
#include "SDL\SDL.h"
class CApp{
private:
bool Running;
SDL_Surface* Surf_Display;
public:
CApp();
int OnExecute();
public:
bool OnInit();
void OnEvent(SDL_Event* Event);
void OnLoop();
void OnRender();
void OnCleanup();
};
#endif // CAPP_H_INCLUDED
put these arguments to the main function. I had this problem too, and I fixed it few seconds ago.
int main(int argv, char** args)
{
}
Try #undef main after all SDL related headers.
Update. This is not a valid solution!
As pointed out by HolyBlackCat, this is a pretty sloppy fix. SDL replaces the main function in order to perform some initialization and/or cleanup that is otherwise not possible, and then calls back to the actual user function.
The interception works by replacing the name of user's main function to SDL_main, with a simple macro
#define main SDL_main
The user's function then ceases to be the entry point for the application, and an entry point provided by SDL is used. The proposed #undef disables the interception recklessly and one should argue that it is not supposed to work at all. For those who successfully compiled and ran an SDL application after this "fix", it must have simply been a platform-dependent coincidence.
The proper solution to the OP's error is making sure that the file containing main gets compiled and linked, and that the function has correct signature. As already posted by others.
The only plausible reason for your problem I can think of is that when you created the file with main in it, you forgot to add it to build targets.
You should see CApp.cpp in the list where my main.cpp is. Right click on it and click Properties. Click on Build tab in the window that pops up. You should see this:
Click OK, hit Ctrl+F11 (Rebuild).
Good luck.
Recently, I had this problem, watched some YouTube videos and also followed the installation guide by LazyFoo and I kept getting the "Undefined reference to SDL_main" error.
I followed everything said here after successfully linking the minGw files to my project properties, but it didn't work until I added to my main function int main (int argv, char** args) and voila, it worked.
using namespace std;
int main(int argv, char** args) { if(SDL_Init(SDL_INIT_VIDEO)<0) {
cout<<"Endl Init Failed."<<endl;
return 1; }
cout<<"SDL Init succeeded."<<endl;
SDL_Quit(); return 0; }
Why it worked is still not clear to me but it worked anyway.
Define SDL_MAIN_HANDLED before you include the SDL libs:
#define SDL_MAIN_HANDLED
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
More info: I'm using the SDL functions without the SDL_main be defined. Is that fine?
This was the first question that pop out to my search.
None of the solutions worked for me.
Eventually I figured it what worked for me. Will post the my answer here to "any fellow wanderer".
My problem was that I tried to compile some old C game not a C++ one.
One part of the solution for me was to rename the main.c to main.cpp
And then to signal to CMake to use CPP compiler for all C files.
Eventually here is a portion of my CMakeLists.txt:
# set minimum cmake version
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)
# project name and language
project(FootballManager LANGUAGES CXX)
# This is mandatory too, because otherwise they are not compiled an linked.
file(GLOB_RECURSE CFILES "${CMAKE_SOURCE_DIR}/*.c")
SET_SOURCE_FILES_PROPERTIES(${CFILES} PROPERTIES LANGUAGE CXX)
# list sources
list(APPEND _sources config.h)
# ... removed some of the other sources for brevity ...
list(APPEND _sources misc.c)
# Link SDL, yes this is SDL1.2
find_package(SDL REQUIRED)
include_directories(FootballManager ${SDL_INCLUDE_DIRS})
add_executable(fm main.cpp ${_sources})
target_link_libraries(fm ${SDL_LIBRARIES})

mingw produces broken .exe

I have installed the newest MinGW suite. My project still compiles without any error but the produced executable is not working. Starting it results in the well known windows xp error message. Paradoxically source code like
#include <stdio.h>
int main()
{
printf("test\n");
return 0;
}
produces a working executable while
#include <iostream>
int main()
{
std::cout << "test\n" << std::endl;
return 0;
}
compiles fine but the executable is broken as described above.
Before i made the update everything worked. So what goes wrong here?
Do you have the libstdc++-*.dll in the path? It may be shared in newer MinGW versions, and std::cout uses it.
A tool like Process Monitor will probably tell you what is actually going wrong in more detail, and possibly even tell you what you need to fix to make it work.

Code Blocks, MinGW, Boost, and static linking issues

I am using Code Blocks with MinGW and am trying to get a simple program to compile with static linking. I have built the Boost libraries using these directions. Everything worked out fine and I was able to successfully compile this simple program (it compiles, I know it doesn't work because it exits before the message is sent to the console, but I just want it to compile).
If I have a DLL in my linker libraries, it compiles fine, but when I switch it with the static .a libraries of the same contents, I get undefined references such as "undefined reference to `_imp___ZN5boost6threadD1Ev'|".
I have no idea what the problem is and can't find the solution. I think it might have to do with linker settings but I can't find information on how to change them. I would be extremely grateful for any help that could be provided.
#include <iostream>
#include <boost/thread.hpp>
void myfunction()
{
std::cout << "this is a thread" << std::endl;
return;
}
int main()
{
boost::thread mythread(&myfunction);
return 0;
}
It's from trying to link statically when the headers are configured for a dynamic link. I explain this for libssh in this question. Poking around in boost/thread/detail/config.hpp makes me think you should #define BOOST_THREAD_USE_LIB, or use the -D flag to do the same.