Procedural parameters in xcode - c

is it possible to get procedural paramters working in xcode? I am working on an assignment for university, and would like to use the debugger in xcode to help me learn whats going on.
I know the code is at the very minimum compilable, because i was able to compile it with gcc, but xcode does not recognize the syntax.
the code i'm trying to run in xcode is:
#include <stdlib.h>
#include <stdio.h>
int main() {
int a = 50;
int b = 100;
void P(int *c, void R(int)) {
void Q(int p) {
printf("%d\n", p);
R(p+100);
}
if (a == b) R(b);
else {c = c + 25;
P(c, Q);
}
}
void Q(int p) {
printf("%d\n", p);
}
P(&a,Q);
}

Xcode uses the clang compiler by default, which does not support nested functions. To use gcc in Xcode, go to your project settings, select your target and change Compiler for C/C++/Objective-C to LLVM GCC 4.2. Now if you compile, gcc will complain, that nested functions are not enabled. Go back to your target settings, LLVM GCC 4.2 Language and under Other C Flags add -fnested-functions. Your project should compile now.

Related

EDK2 Shell Application using Variable Arguments built with GCC causes Page Fault when run

I am having issues with using variable arguments under EDK2 (x64 shell application) when built under a Linux host with gcc. Program builds but when executed it will cause a page fault at the point VA_ARG() is executed.
The same code when built under a Windows host with VS2015 works without issue.
This seems related to GCC bug 50818 but I can find no solution.
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/PrintLib.h>
#include <Library/ShellCEntryLib.h>
VOID PrintInts(UINTN n, ...)
{
VA_LIST vl;
VA_START(vl, n);
Print(L"Printing integers:");
for (UINTN i=0; i<n; i++) {
UINTN val = 0;
val = VA_ARG(vl, UINTN);
Print(L" [%d]", val);
}
VA_END(vl);
Print(L"\n");
}
INTN EFIAPI ShellAppMain(IN UINTN Argc, IN CHAR16 **Argv)
{
UINTN a = 3;
UINTN b = 10;
UINTN c = 9;
PrintInts(3, a, b, c);
return 0;
}
I have found a fix and that is to define the function with the EFIAPI tag and this fixes the issue, i.e.
VOID EFIAPI PrintInts(UINTN n, ...)
From this link:
When creating a 32-bit UEFI application, EFIAPI is empty; GCC will compile the "efi_main" function using the standard C calling convention. When creating a 64-bit UEFI application, EFIAPI expands to "__attribute__((ms_abi))" and GCC will compile the "efi_main" function using Microsoft's x64 calling convention, as specified by UEFI. Only functions that will be called directly from UEFI (including main, but also callbacks) need to use the UEFI calling convention.
Also it only seem to be an issue with GCC as if I use CLANG I do not need to specify EFIAPI.

How to link a lib written in D to use it with a program written in C, under Windows, using MinGW GCC?

I would like to use a library written in D for a C program compilable with MinGW GCC, for Windows. Here are the codes:
dll.d
extern (C) int dsquare(int n) nothrow
{
return n * n;
}
main.c
#include <stdio.h>
int main()
{
int res = dsquare(6); // Expect '36'
printf("res = %d\n", res);
return 0;
}
There is a tutorial on D's site, but it seems to target only Linux. Indeed, no explanation is given for creating such a dynamic D library for Windows and MinGW users.
D's documentation also says that the -shared option should generate a DLL version of the D code, but in my case, it generates an executable, I don't know why.
Also, anything that seems to generate files to be linked targets MVSC formats and nothing seems to be suitable for MinGW GCC compilers.
So, how can I generate a "GCC-friend" DLL with D, so that I can link it to my C program without having to use another compiler, such as GDC or LDC, via gcc main.c -o main -ldll -L. (I guess)?
I attached link with short explanation. Link D onto C is not so straightforward as C to D. Check D.org page here:
https://dlang.org/spec/betterc.html

WindowsDefender Trojan warning after compiling c program with string.h and stdio.h?

As you will see I am not good in c programming, still learning but my WindowsDefender is yelling at me for compiling this code with MinGW from Codeblocks IDE.
#include <stdio.h>
#include <string.h>
int main(void){
char *strings[] = {"test", "test"};
char *newStr;
int i;
for(i=0;i<2;i++){
strcat(newStr, strings[i]);
printf("%s\n", newStr);
}
return 0;
}
Can you help me with that I don't know what this is about. This can't be a normal issue mh?
I have been having this for several days recently when I compile simple programs in C in Microsoft Visual Studio 2019 Community edition. For example this one.
#include <stdio.h>
int main() {
int a = 10;
for (int i = 0; i < 5; i++) {
a--;
printf("i = %d\n", i);
if (a == 8) break;
}
}
The problem isn't with your program (or mine) but Windows Defender has become overly paranoid and is erroneously identifying normal compiled code. I get the same Trojan warning as you. The danger is if we turn off Defender for crying wolf that a real virus could slip onto an unguarded Pc. I'm tempted to switch to a different a/v like AVG or switch into hyper-V and compile programs on Ubuntu with GCC or clang.

Compile error using sqrt() in c

I have a problem using sqrt() in c.
I'm trying to do something like this:
int a;
a = sqrt(9);
etc
I'm restricted to use:
gcc -ansi -Wall -pedantic oneFile.c anotherFile.c thirdFile.c -o outputFileName
How can I make this compile without using the -lm command?
Yes, I have #include !
Is there any way around this and still use sqrt()?
Thanks
You can't make it compile without -lm. That's an instruction to the linker to compile against the built-in math library. It isn't enough to say #include <math.h>, because that's only a header file - there's no code in there, that simply tells the compiler what the functions you're using look like. You still need to actually get the implementation of that function into your code, and -lm basically tells the linker look in libm, check to see if it has any functions that we haven't found yet. If you don't look in there, you'll never be able to actually execute sqrt because the code simply isn't in your program.
If you're working on a homework assignment and are restricted to using that command line, then it's possible that part of your assignment is to not use anything from the math library, and so you may be expected to consider an alternate approach.
just try with this function, If you don`t want to use library.
Sq_root(n)
{
count=0;
int i = 0;
for(i=1;sum<=n;i+=2)
{
sum+=i;
count++;
}
return (count);
}
This will work, without any math library.
use #include <math.h> in header
else use user define function
int int_sqrt(int x){
int s, t;
s = 1; t = x;
while (s < t) {
s <<= 1;
t >>= 1;
}
do {
t = s;
s = (x / s + s) >> 1;
} while (s < t);
return t;
}

C Build error when getting the value of sin()

I have recently started learning C as a side project. I am working under OpenSuse with the latest NetBeans using the GCC as toolset for compiling.
One of the very first programs that I made was this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
*
*/
int main(int argc, char** argv) {
double rad = 1;
double result = 0;
result = sin(rad);
return (EXIT_SUCCESS);
}
This is a simple, no-brainer example that should have worked without a problem. However, I get a Build Error: Exit code 2(error in line 18, undefined reference to sin) when trying to compile.
Interestingly enough, if I remove the assignment of the value of sin(rad) to result OR replace rad with a hard coded value, the program compiles just fine.
What am I doing wrong here?
In C, you need to link to the math library:
Add this to the command line options:
-lm
Be sure that your are linking with the math library.
$ gcc myprog.c -lm

Resources