Undefined reference to WinMain#16 when using SDL - c

I've been having a lot of trouble getting everything working so that I can start developing on Windows, as apposed to Linux, which is what I normally use when coding. I'm having a rather bizarre issue when trying to compile an SDL program. As soon as I include the SDL library, the program refuses to compile, giving me this error:
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../libmingw32.a<main.o>: In function 'main':
C:\MinGW\msys\1.0\src\mingwrt/../mingw/main.c:73: undefined reference to 'WinMain#16'
collect2: ld returned 1 exist status
I am using MinGW on console.
To give an example, using
gcc -o test main.c
This compiles fine:
#include <stdio.h>
#include <stdlib.h>
int main(int argv, char **argc)
{
printf("Hello, world!\n");
return 0;
}
But as soon as I add #include (even without any SDL functions being called) I get the error mentioned above
Using:
gcc -o test main.c -lSDL
This fails to compile:
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
int main(int argv, char **argc)
{
printf("Hello, world!\n");
return 0;
}
Any help would be greatly appreciated! I read that this was a common issue for people who forget to have a main function, but obviously that's not my issue. I also heard that WinMain is the main function used when dealing with Windows graphical programs, but that's never been an issue for me in the past when I used to develop in Windows more.

I did a little bit of searching for some more information on this error, and I found this page which includes the following informaion:
The only trick in getting this to compile now is to add the include path (eg: -I../SDL/include), the linker path (eg: -L../SDL/lib), and then finally adding the libraries themselves in the right order. Use:
-lmingw32 -lSDLmain -lSDL
Also, don't forget to add the -mwindows flag, if your IDE doesn't add it automatically (in addition to whatever other libraries you want to link). If you don't put them in the right order, you'll get a linker error complaining about the missing symbol WinMain#16.
Try recompiling with those flags above and see whether that makes a difference.

Related

Unable to compile Hello World

Sorry if this is a repeat question, but I couldn't find an answer that worked.
I'm writing a Hello World C program for the first time in a long time. I'm fairly certain the code is right, but it won't compile.
Running MAC OS 10.13.6 and I just downloaded XCode last week. The program compiles into an object file using
cc -c test.c -o test.o
without a problem. However, I can't create an executable using
cc test.o -o test
Here's the code:
#include <stdio.h>
int Main()
{
printf("Hello World");
return 0;
}
When I go to create the executable, I get
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
I'm guessing I need to add some compiler flags but can't figure out which ones.
It sounds like you are just starting out in your journey into c code. This is a great opportunity to learn about c, compiling, and linking.
What you have written can compile just fine into an object file (containing a function called Main()). However, to link into an executable that can run from your OS, you need to define an entry point which is assumed to be a function called main (case sensitive as John mentioned above).
Check out this resource: http://www.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html for an explanation of what the gcc compiler gets up to behind the scenes.
You just need to make a couple of small changes to get your code to work as expected:
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
return 0;
}
Main needs to be lowercase main.
While int main() will work, int main(void) is technically more accurate (see
void main(void) vs main())
You probably want a line new line after Hello, world!, so add the \n. (https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm)

gmp strange behaviour that doesn't let me compile new project

I've installed on my ubuntu gmp using this command:
sudo apt-get install libgmp3-dev
and it worked fine.
Now I'm trying to create a new project put simply writing
#include "gmp.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(){
mpz_t num;
mpz_init(num);
printf("%s\n",mpz_get_str (NULL, 10, num));
mpz_clear(num);
return 0;
}
give me
> gcc -lgmp mil.c /tmp/ccHvV9kT.o: In function `main':
> mil.c:(.text+0x1f): undefined reference to `__gmpz_init'
> mil.c:(.text+0x35): undefined reference to `__gmpz_get_str'
> mil.c:(.text+0x49): undefined reference to `__gmpz_clear' collect2:
> error: ld returned 1 exit status
I just copy-pasted the code of my previous project and I get the same error(in all the function that I created), but compiling my old project I don't get any error.
What is my problem???
Order of arguments to gcc matters a big lot.
Try to use (you want warnings and debug info, so)
gcc -Wall -Wextra -g mil.c -lgmp -o milprog
Then run ./milprog. You may want to use the gdb debugger on it, with
gdb ./milprog
and you may want (for benchmarking purposes) to ask the compiler to optimize, by adding (before -g) something like -O2 -march=native
Learn to use GNU make (or some other build automation tool, like ninja), see this.
Be sure to use a version control system like git.
BTW, I find more logical and more elegant to include "gmp.h" after (not before, as you did) the inclusion of standard headers (like <stdio.h>).

How to organize Header files

Using header files in this way gives me the error "undefined reference to somefunc". What is the proper way to make sure somefunc.c is seen so this error doesn't occur? It seems simply including somefile.h in main.c isn't enough to see the definitions in somefile.c
main.c
#include "somefile.h"
int main() {
somefunc();
return 0;
}
somefile.h
#ifndef SOMEFILE_H
#define SOMEFILE_H
void somefunc();
#endif
somefile.c
#include <stdio.h>
#include "somefile.h"
void somefunc() {
printf("hello\n");
}
I don't understand why I am getting errors because this is the same manner in which they are used in tutorials and videos i've been viewing while looking for an answer. The code above is an answer given earlier but it is still has the same error.
Undefined reference to somefunc is a linker error, not a compiler error.
This means that, although when compiling main.c the header somefile.h is found, you are not compiling the file somefile.c together with main.c. So when linking occurs the linker is not able to find the implementation of somefunc in any object file to resolve the call from main().
If you are using GCC or Clang just compile both source files to your command, eg
gcc somefunc.c main.c -o output
If you are using an IDE instead, make sure that somefile.c is compiled together with main.c when building the application.
This doesn't appear to be a problem with the header file.
This appears to be a problem in linking, which depends on how you build the project. If you use an IDE, it means that somefile.c is not included in the project. If you're using make and a makefile, it means that somefile.c is not listed in the makefile, or at least not included for the linker. If you're building at the command line (not using make or some build tool, but using gcc), then you're not including somefile.c in the command.
The undefined reference error means the linker couldn't find the code in somefile.c, because the linker didn't know to include it.

Linux Mint Eclipse GLFW 3 Setup problems

I have, in my opinion, installed and configured GLFW 3 correctly,
my compiler tells me otherwise.
glfw3.h and glfw3native.h are in urs/local/include/GLFW/.
libglfw3.a is in urs/local/lib/.
In Eclipse, i configured in Project Propreties->C/C++ Build->Settings->GCC C Linker->Libraries the following values : GL, GLU, m, rt, pthread, m, glfw3, X11, Xxf86vm, Xrandr and Xi.
In my project, in Test.c i have the following test code :
/*
============================================================================
Name : Test.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
if (!glfwInit())
exit(EXIT_FAILURE);
return EXIT_SUCCESS;
}
So in my opinion and what i have read one the internet, it seems an alright setup.
When i compile it it gives this :
/usr/local/lib/libglfw3.a(window.c.o): In function `glfwCreateWindow':
window.c:(.text+0x724): undefined reference to `glClear'
//usr/local/lib/libglfw3.a(glx_context.c.o): In function `getFBConfigAttrib':
glx_context.c:(.text+0x4d): undefined reference to `glXGetFBConfigAttrib'
//usr/local/lib/libglfw3.a(glx_context.c.o): In function `chooseFBConfig':
glx_context.c:(.text+0x7a): undefined reference to `glXGetClientString'
glx_context.c:(.text+0xe5): undefined reference to `glXGetFBConfigs'
//usr/local/lib/libglfw3.a(glx_context.c.o): In function `createLegacyContext':
glx_context.c:(.text+0x41f): undefined reference to `glXCreateNewContext'
Reading this i came to the conclusion that it didn't find OpenGL, but i already made OpenGL projects before and it works, so why can GLFW find it?
Thanks.
After some tickering around and praying for this problem to go away for a couple hours, and days. I have found the solution... and i don't fully understand why linking is so hard.
What i did is just try different Linker Libraries option,
This order is the one that worked for me :
-lglfw3 -lGL -lGLU -lX11 -Xxf86vm -lXrandr -lpthread -lXi -lm
Hope this helps others.

Ubuntu C program loadable module and undefined symbols

Very new to linux in general and trying to build a loadable module for use in zabbix, which works, but trying to build a simple shell program for testing it. That means this module needs to be loaded dynamically.
Sharable module SNMPmath is built with this:
gcc -shared -o SNMPmath.so $(CFLAGS) -I../../../include -I/usr/include/libxml2 -fPIC SNMPmath.c
That works fine for zabbix.
The test program (TestSO.c) uses
lib_handle = dlopen("./SNMPmath.so", RTLD_NOW);
to load this image dynamically, and when it does, it is missing symbols including init_snmp which come from the net-snmp package which is referenced in the SNMPmath loadable module.
My question is both general and specific. What's the right approach -- is this library called by the loadable module supposed to be forced into the loadable module? Is it supposed to be forced into the test program (despite having no compile-time reference to it)? Or is it supposed to be dynamically loaded, itself, in the loadable module (which seems to contradict what I'm seeing in other examples)?
And in either case, how is the GCC command modified to include net-snmp? I've tried variations of whole-archive, no-as-needed, listing what I think is the library (/usr/lib/x86_64-linux-gnu/libsnmp.a) with various compiler options with no effect (or occasionally errors). Also tried linking to the .so version of that (no effect). So a hint as to the proper GCC command to include the library would be very helpful.
Here's one iteration of attempts at linking the main program:
gcc -rdynamic -o TestSO -I../../../include -I/usr/include/libxml2 TestSO.c -ldl -Wl,--no-as-needed /usr/lib/x86_64-linux-gnu/libsnmp.so
I've found numerous examples of loading modules, but they all load a simple routine that does not itself have undefined symbols that need satisfying.
Recap:
TestSO.c
==> Loads with dlopen SNMPmath.c
==> needs to refer to net-snmp routines like init_snmp
Pointers to examples or explanation welcome, I realize I'm missing something fairly obvious.
EDIT AFTER FIRST COMMENTS TO INCLUDE:
I have it sort of working now, but would appreciate a sanity check if this is correct. I pruned it down so as to show the whole code. Here is the code to produce the SO:
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <string.h>
int zbx_module_SNMPmath_avg(int i, int j)
{
init_snmp("snmpapp"); // initialize SNMP library
return 1;
}
Here is how it is compiled (note slight name change):
CFLAGS=-I. `net-snmp-config --cflags`
SNMPmath_small: SNMPmath_small.c
gcc -shared -o SNMPmath.so $(CFLAGS) -I../../../include -I/usr/include/libxml2 -fPIC SNMPmath_small.c -Wl,--no-as-needed,/usr/lib/x86_64-linux-gnu/libsnmp.so
Then here is the main program:
#include <stdio.h>
#include <dlfcn.h>
#include <dlfcn.h>
#include <string.h>
int main(int argc, char **argv)
{
void *lib_handle;
int(*fn)(int req, int ret);
int x;
char *error;
lib_handle = dlopen("./SNMPmath.so", RTLD_NOW);
if (!lib_handle)
{
fprintf(stderr, "Error on open - %s\n", dlerror());
exit(1);
}
else
fprintf(stderr,"Successfully loaded module\n");
fn = dlsym(lib_handle, "zbx_module_SNMPmath_avg");
if ((error = dlerror()) != NULL)
{
fprintf(stderr, "Error on dlsym %s\n", error);
exit(1);
}
else fprintf(stderr,"Successfully called dlsym\n");
// testing
int req, ret;
req=1;
ret=1;
x=(*fn)(req, ret);
printf("Valx=%i\n",x);
dlclose(lib_handle);
return 0;
}
And finally this is built with:
TestSO: TestSO.c
gcc -rdynamic -o TestSO -I../../../include -I/usr/include/libxml2 TestSO.c -ldl
This now will run as expected. I found that linking against the netsnmp library's so file when building my so seemed to work.
But is this the correct sequence? And/or the preferred sequence?
Ps. Off to read the paper in the first proposed answer.
Pointer to explanation: Drepper's paper: Howto write shared libraries
But we need more source code (and the commands used to compile it), and the real error messages (e.g. as given by dlerror() after dlopen call) to help more.
(See also this answer)
You might want to add some libraries like -lsomething (I don't know which, you probably do know!) to your command gcc -shared which is building SNMPmath.so .... You don't want to link a static library like libsnmp.a to it (you should link shared libraries to your SNMPmath.so shared object).

Resources