I'm starting out on SDL using CodeBlocks IDE. I got an error undefined reference to 'SDL_main'. I researched and found out that you need to have the parameters
int argc, char* args[]
in the main(). I added those and the code compiled and worked. My question is, how important are the parameters for the linker to be able to work?
Main parameters have to be there because SDL defines main internally as ......SDL_main(int argc, char *argv[])... depending on the system and does some initialization.
Just include main parameters, it doesn't matter if you're not using them.
Related
void f();
int main(int argc, char** argv)
{
if (f)
{
// other code
}
}
With VS2017, the linker complaint about unsolved external symbol, while it works with GCC. According to C99 spec, is it valid? Or it's implementation detail?
C standard requires that every symbol should be defined exactly once in a correct program, but does not require any diagnostic if the rule is not observed. So if you declare a function that is never defined in any compilation unit any use of that function is beyond C specification.
The gcc compiler is known to have plenty of extensions, some of which are also accepted by clang. If you know that you will only use gcc, you can use them, if you want to write portable programs you should not.
GCC regards a declared function as always existent, even with -O0, and therefore optimizes the code to:
#include <stdio.h>
int main(int argc, char** argv)
{
printf("Y\n");
}
If you add common warning options to the compiler command line, GCC will warn you about this, too.
It's common to check if the weak function is defined(usually user
defined callback) in embedded code.
No, it is never done as it cannot be done. Your code may have some pointers to functions and you can check if those pointers are not NULL (ie have been assigned with some function pointer references, but cant check if those references are correct).
My overall goal is to use a C model inside my MATLAB code. The C model is large (over a dozen .c files, which are all run from cModel.c) and can be successfully compiled then run in the terminal by
make cModel
cModel.x startingfile.inp
as the C model is correctly built for normal C compilers.
However, MATLAB's mex function is not compiling this C code. I'm a total novice with mex and I am pulling my hair out trying to understand what the problem is.
I think (and reading some similar problems on stackoverflow backs this up) that the problem is around creating a mexFunction. My attempt currently is
/*function AA_mexWrapper.c*/
/*Include the MATLAB mex header*/
#include "mex.h"
/* The gateway function */
void mexFunction( )
{
/* Main() of the C Model*/
cModel(); /* cModel writes files. We don't care about the nonexistant returned variables*/
}
This generates the error (using mex AA_mexWrapper cModel) :
Error using mex
/Users/Filepath/ cModel.c:215:5: warning:
implicit declaration of function 'main' is invalid in C99 [-Wimplicit- function-declaration]
main(int argc, char **argv);
^
/Users/Filepath/ cModel.c:215:10: error:
expected expression
main(int argc, char **argv);
^
1 warning and 1 error generated.
What is MATLAB doing and how do I fix it? I really just need it to treat cModel.c like a normal C compiler would.
PS. I have no idea what (int argc, char **argv) are in the C code. They are totally undefined, presumably they come from the optional user input of a filename containing non-default parameters for the model.
PPS. I will need to run the C model inside matlab by pointing it to a text file containing various model options. I hope that MATLAB can deal with this, but I'm starting to have my doubts...
You cannot call an executable like a function; the name of the executable is not "exported" the way you might think.
How about a simple solution? build your executable outside MATLAB and ask MATLAB to just run it; here's a piece of code that would do that (assuming that the cModel.x is in the same folder as the script/function that calls it):
system('./cModel.x startingfile.inp');
I have a file hello.exe file path is D:\test\hello.exewhich is a simple hello world program (tested ok).
I have another program proc.c file path is D:\test\proc.c with code as follows:
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<process.h>
#include<errno.h>
#include<string.h>
main(int argc,char *argv[])
{
int ret;
ret=execl("D:\\test\\hello.exe","D:\\test\\hello.exe");
if(ret==-1)
printf("%d:\t%s",ret,errno);
}
The program hangs (windows dialog says The program has stopped working)!!!
I even tried D:/test/hello.exe as param to execl(), but the same...
Where am I going wrong???
Please someone provide me the right syntax? Please prvide me example codes using various functions of process.h with MinGW under windows. Web tutorial links are acceptable as well.
Thanks a lt !!!
You have to include a library,
#include<unistd.h>
Because it is defined in this library So if you don't include this you will get AN error.
and correct your code because you have written "exec" not execl.
int main()
{
execl("D:/test/hello","D:/test/hello",0);
return 0;
}
If you compiled with warnings, you'd notice that errno is an int, but you're trying to use it with printf as a string, which means it is being used as a pointer. Memory address 0xFFFFFFF2 (or whatever the hexadecimal value of errno is) probably isn't valid.
Aside from that, you need to end your execl invocation with a NULL pointer. Otherwise the function will keep thinking it has more command line arguments, all of which are strings, until the first NULL it finds in memory or until the first invalid memory access, whichever comes first. Because copying the strings involves dereferencing a pointer, it will read the address made up of unknown bytes and attempt to dereference the pointer at that address repeatedly. If the pointer cannot be dereferenced, meaning accessing the address for reading data from it cannot be done, it will crash your program.
In the future, please compile with -Wall.
I've been using the default template in Code::Blocks to create a DLL, but when I compile, I get the undefined reference to'WinMain#16' error.
I Googled out this error and it seems that the main() I'm using is for a Window application rather than console application but there's no clear solution to this problem.
Now, do I just replace this line
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
by this
int main()?
Also, I want to insist that whatever alternative causes the least security gap and/or is the most correct solution.
Thanks!
I did encounter this same error sometime ago, just replacing some part of the code fixes it. I was using SDL in my program. This was the main function:
int main(int argc, char** argv)
I relaced it with the windows main function:
int WinMain(int argc, char** argv)
but it did end up with giving a warning for the same however the code runs.
Can the main() function be declared static in a C program? If so then what is the use of it?
Is it possible if I use assembly code and call the static main() function myself (consider embedded programs)?
No. The C spec actually says somewhere in it (I read the spec, believe it or not) that the main function cannot be static.
The reason for this is that static means "don't let anything outside this source file use this object". The benefit is that it protects against name collisions in C when you go to link (it would be bad bad bad if you had two globals both named "is_initialized" in different files... they'd get silently merged, unless you made them static). It also allows the compiler to perform certain optimizations that it wouldn't be able to otherwise. These two reasons are why static is a nice thing to have.
Since you can't access static functions from outside the file, how would the OS be able to access the main function to start your program? That's why main can't be static.
Some compilers treat "main" specially and might silently ignore you when you declare it static.
Edit: Looks like I was wrong about that the spec says main can't be static, but it does say it can't be inline in a hosted environment (if you have to ask what "hosted environment" means, then you're in one). But on OS X and Linux, if you declare main static, then you'll get a link error because the linker can't find the definition of "main".
You could have a static function called main() in a source file, and it would probably compile, but it would not be the main() function because it would be invisible to the linker when the start-up code (crt0.o on many (older) Unix systems) calls main().
Given the code:
static int main(int argc, char **argv)
{
return(argv + argc);
}
extern int x(int argc, char **argv)
{
return(main(argc, argv));
}
GCC with -Wall helpfully says:
warning: 'main' is normally a non-static function
Yes, it can be done. No, it is normally a mistake - and it is not the main() function.
No you cannot do it. If you will do it you will be unable to compile your program. Because static function is only visible within the same file, so the linker will no be able to find it and make a call of it.
As others have said, no it can't. And that goes double if you ever intend to port your code to C++, as the C++ Standard specifies that main() need not actually be a function.
C has two meanings for 'static'...
static for a local variable means it can be used globally.
static for a global variable means is can only be used in the current file.
static for functions has the exact same impact as denoting a global variable as static ... the static function IS ONLY VISIBLE IN THE CURRENT FILE ...
Thus main can NEVER be static, because it would not be able to serve as the primary entry point for the program.