from C to assembly - c

how can I get assembly code from C program I used this recommendation
and I use something like this -c -fmessage-length=0 -O2 -S in Eclipse, but I've got an error, thanks in advance for any help
now I have this error
atam.c:11: error: conflicting types for 'select'
/usr/include/sys/select.h:112: error: previous declaration of 'select' was here
atam.c:11: error: conflicting types for 'select'
/usr/include/sys/select.h:112: error: previous declaration of 'select' was here
this my function
int select(int board[],int length,int search){
int left=0, right=length-1;
while(1){
int pivot_index=(right+left)/2;
int ordered_pivot=partition(board,left,right,pivot_index);
if(ordered_pivot==search){
return board[ordered_pivot];
}
else if(search<ordered_pivot){
right=ordered_pivot-1;
}
else{
left=ordered_pivot+1;
}
}
}

Eclipse is still treating the output as an object file
gcc -O0 -g3 -Wall -c -fmessage-length=0 -O2 -S -oatam.o ..\atam.c
is generating assembly like you want, but confusingly storing it in atam.o because of the -oatam.o passed to GCC (normally you would store assembly in atam.s). The next command:
gcc -oatam.exe atam.o
Attempts to link atam.o and generate an executable, but atam.o is an assembly source, not an object file, so the linker fails. You need to tell eclipse not to run the second command (and you probably want to change the output filename of the first)

The error is happening because select is a Unix system call and your definition is clashing with the declaration in the relevant system header. You need to rename your function.

-S instructs the compiler to not go through with the actual compilation and linking step and stop after emitting the assembly. On the other hand you're also telling the compiler to compile your file on the same line (in addition to other conflicting settings).
Try this:
gcc -O0 -S ../atam.c
Optimizations will take the assembly file generated farther away from your source code, so I instructed gcc to turn off optimizations. Also don't run the linker.

Related

Error while compiling C program with gcc in VSCode

I wanted to compile the below code in VS Code, but I'm fetching this error using "code runner". I've looked up everywhere, but it didn't solve my issue.
I want to implement this T(n) = 2T(n/2) + nlog(n)
q2.c
// b. T(n) = 2T(n/2) + nlog(n)
#include <stdio.h>
#include <math.h>
int func(double n)
{
return (2*func(n/2) + n*(log(n)));
}
int main()
{
double n, result;
printf("Enter the value of 'n' \n");
scanf("%lf",&n);
printf("Hey");
result = func(n);
printf("%lf \n",result);
printf("Hey");
return 0;
}
Console:
user#user-H310M-DS2:~/Desktop/C programming/Assignments$ cd "/home/user/Desktop/C programming/Assignments/" && gcc q2.c -o q2 && "/home/user/Desktop/C programming/Assignments/"q2
/tmp/ccnNXN3L.o: In function `func':
q2.c:(.text+0x3a): undefined reference to `log'
collect2: error: ld returned 1 exit status
Visual studio code has nothing to do with your issue, you are not compiling with it. Because it is an IDE (or source code editor), not a compiler. I guess you are using it on some Linux or POSIX system. BTW my preferred source code editor is GNU emacs. So your IDE is running some compilation commands (and you need to understand which ones and what these commands are doing). You could run these commands in a terminal (and that actually might be simpler).
As your console logs shows, you are compiling with GCC. Some gcc command has been started (by Visual studio code probably).
Read carefully about Invoking GCC. Order of arguments matters a lot!
You should compile your code with
gcc -Wall -Wextra -g q2.c -lm -o q2
Let me explain this a bit:
gcc is your compiler front-end (the actual compiler is cc1 but you never use it directly; you ask gcc to run it)
-Wall asks for almost all warnings
-Wextra asks for extra warnings. You'll be happy to get them
-g asks for debugging information in DWARF. You really want to be able to use the gdb debugger, and gdb practically needs debugging information.
q2.c is the source file of your sole translation unit
-lm is for your math library. You are using log(3) and its documentation mention that.
-o q2 tells gcc to put the executable in q2 (the actual work is done by the ld linker invoked by gcc)
How to configure visual studio code to use that command is your business. You could otherwise type the above command in a terminal. Then you can run your q2 program by typing ./q2 in a terminal for your shell (and you could use gdb on it).
Notice that gcc is starting other programs (like cc1, as, ld). If you want to understand which ones, insert -v after gcc in the command above.
Be sure to read the documentation of every function you are using (so read printf(3), scanf(3), log(3) at least...) and of every program you are using (e.g. of gcc and of Visual studio code).
Once you'll write bigger programs made of several translation units (e.g. foo.c, bar.c, gee.c), you would want to use some build automation tool (because compiling all of them every time with gcc -Wall -Wextra -g foo.c bar.c gee.c -lm -o qqq is possible, but inconvenient). You could learn to use GNU make (or ninja).
Read How to debug small programs. Don't expect your program to work as you want at first.
BTW, study the source code of some existing free software programs (but start with simple projects, e.g. on github, of less than a hundred thousand lines). This could teach you many useful things.
I'm not sure how VSCode compiles programs, but since it uses GCC, it's likely that you need to link the math library libm when compiling, by supplying an argument -lm to GCC.
Just a tweak to code runner's settings.json under file->preferences->settings of VS Code :
I've added the below line
"code-runner.executorMap":
{
"c": "cd $dir && gcc -Wall -Wextra -g $fileName -lm -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
}
It's working now.

Please help me to compile this C program? [Linux]

I am using Linux now, and trying to compile this by gcc BUT.......
this is my truly simple code:
#include <stdio.h>
int main(){
printf("Hello world\n");
return 0;
}
and this is so much weird output:
./try.c: line 3: syntax error near unexpected token `('
./try.c: line 3: `int main(){'
why is it?
I have tried the right way to compile it, such:
gcc file_name.c -o file_name and other types of way of compiling
chmod +rwx file_name.c
./file_name.c
but still I got that result, why?
You have to compile the code first.
Follow these steps.
gcc try.c -o try.out
to compile the code. The -o option is given to give a custom name to the executable that will be produced.
Then, you can run it by typing
./try.out
To run the executable.
Be informed though, that there are a number of command line options that you can use to get the information about your code and add more functionality. See this page for more information.
You are trying to execute the .c file, remove the trailing .c from the name of the file you want to execute.
Like this:
gcc -Wall -Wextra -pedantic -Werror -o executable file_name.c
You should not need to make it executable, it should already be executable since the compiler will do that.
./executable
As you see, I've passed some parameters to gcc to let it help in diagnosing problems, sometimes these problems are caused by your lack of knowledge and some other times because you write code quickly and miss some details. So using them is good (although compilation is slower, but that doesn't matter if you have a good and fast machine, wehreas having issues in the code does matter).
The meaning of these flags are as follows
-Wall Enable all warnings. Really some are not enabled, but most are.
-Wextra Enable extra warnings.
-pedantic make the compiler pedantic, i.e. stick strictly to the desired (default for this version of gcc) standard.
-Werror Consider that warnings are errors.
Also, you could have guessed this if you see what the error says
./try.c: line 3: syntax error near unexpected token `('
./try.c: line 3: `int main(){'
as you can see the shell is trying to execute the source code as if it was a shell script, so you can immediately notice that this is not the executable file generated by gcc, and then you would notice the .c in the file name.
Try
gcc try.c
./a.out
Compiles the code and runs it. Please read the manual page for gcc and there are many delights to behold (extra checking etc)

C compilation errors: undeclared (first use in this function)

On the PI, I needed the i2c.so library using this git: https://github.com/silentbobbert/pi_sensors. When running makefile from this git to get the i2c.so, i received this error:
Here are the .c and the .h files:
https://github.com/silentbobbert/pi_sensors/tree/master/Info/LinuxInterface
For reference, here is the contents of makefile:
SHELL = /bin/sh
CC = gcc
FLAGS = -c -Wall -Werror -fpic
DEBUGFLAGS = -O0 -D _DEBUG
RELEASEFLAGS = -O2 -D NDEBUG
TARGET = i2c.so
SOURCES = $(shell echo *.c)
HEADERS = $(shell echo *.h)
OBJECTS = $(SOURCES:.c=.o)
PREFIX = $(DESTDIR)/usr/local
BINDIR = $(PREFIX)/bin
all:
$(CC) $(FLAGS) $(RELEASEFLAGS) $(SOURCES)
$(CC) -shared -o $(TARGET) $(OBJECTS)
as others have said, this is C code not C#.
Anyhow, you have two errors (and they do not relate directly) to the makefile but rather your compilation environment and the code itself.
OK, so how to approach something like this. First notice the first line in your screen-capture, that is the command that is being executed that is generating the error messages, I'll reproduce it here for you;
gcc -c -Wall -Werror -fpic -O2-D NDEBUG i2c_get.c i2c_set.c i2cbusses.c i2cset.c util.c
With this command we are compiling (note the -c flag) a bunch of source files into a single object file, the presence of the `c' flag implies that no linking is performed here. This is relevant so we know where along the tool-chain we are, and the type of errors we can expect (typically either syntax errors or missing header files).
The first error;
i2cset.c: In function 'check_funcs'
i2cset.c:56:2 error: implicit declaration of function 'iotcl' [-Werror=implicit-function-declaration]
is kinda saying, "hey, I can see that ioctl is a function, but you haven't told me anything about it so I'm going to assume that its signature is int ioctl() — a function with an indeterminate (but fixed, not variadic) argument list that returns an int". Given that you are compiling on a Linux-based system, adding #include <sys/ioctl.h> to the top of the file should fix this error.
The second error;
i2cset.c:63:7: error: 'I2C_SMBUS_BYTE' undeclared (first use in this function)
is related to the first error; and it is kinda saying 'hey, you haven't told me anything about I2C_SMBUS_BYTE'. Again, the most common reason for seeing this error is a missing header file. Looking at the source files you've provided a link to, it seems that I2C_SMBUS_BYTE is defined in
the header file i2c-dev.h, which however appears to be included in i2cset.c by: #include <linux/i2c-dev.h>.
At this point I'd insure that your compiler (gcc) can find the header file. From the error messages you are getting, I'm guessing that it is not, but you should be seeing an error message from the compiler about not being able to find the file. Also, if the file is on your system check to see if has the appropriate contents as compared to the git site.
Finally, the remain errors that you are seeing should be fixed as well as they are all basically the same thing.

How can I know in which step (Pre-Processor,Compiling, Linking) my program failed to compile?

How can I know in which step (Pre-Processor,Compiling, Linking) my program got compilation failure?
For example, I wrote a program of 3 source files: a.c ,b.c, c.c and all three of them included the header file, h.h, which contains all the prototypes of all the source files, but I also implemented one function, in the header file. I know it's wrong to do, but I wonder on what stage did the program crash, Is it the linking or the compiling, I got an error message, Is this say that the problem is at the linking stage? (otherwise I'll just get a red underline on the mistake from the compiler?)
Where can I read about linking and what I shouldn't do regarding headers and source files and linking errors?
(I work in Linux, with Eclipse)
You find the location of the error by separating compiling and linking. An error due to the precompiler is unusual and also found during compiling. This is how compiling works on the command line:
Compile step:
gcc -c -o a.o -pedantic -Wall a.c
gcc -c -o b.o -pedantic -Wall b.c
gcc -c -o c.o -pedantic -Wall c.c
link step:
gcc a.o b.o c.o -o prog
Of course you need to specify whatever other flags that are necessary (e.g. -l for linking to a library etc.)
Then read the errors and warnings (!) carefully and you shall find the problem.
Your program crashes only once you have pre-procesed, compiled and linked your program to generate an executable that you can run. A crash is a run-time error.
Re preprocessor/compilation/linking:
You didn't specify what platform/environment you are working in, but in a Linux/Unix environment it is easy to determine if you are getting problems with the linker as you usually get a message with ld.
Compilation errors usually syntax related and easy to identify that way (e.g., mismatched parens, missing semi-colons, etc) (Aside, as a general rule, I would recommend you always compile with the highest warning level, and then consciously determine which warning messages to ignore.)
I am not at a system were I can try it out, so I'm not sure of the pre-processor throws out specific easily identifiable error/warning messages, or if they just get passed on to the compiler and get spit out at that stage.
None of these steps crash your program, that only happens (if it does) after all these steps have been completed successfully.

Compiler is able to find function without matching .h file is updated?

I'm writing a C University project and stumbled upon a compiler behavior which I don't understand.
In this file http://code.google.com/p/openu-bsc-maximveksler/source/browse/trunk/20465/semester/tasks/maman14/alpha/maman14/assembler/phaseOne.c?r=112 I've added a call to function named freeAsmInstruction(). This function is defined in file named lineParser.c, yet I haven't updated the matching lineParser.h header file to include this function declaration.
Why does this code compile? I would expect that gcc would fail to compile phaseOne.c until the correct lineParser.h is updated with the declaration of freeAsmInstruction().
I would appreciate an explanation.
Thank you,
Maxim
The GCC compiler is assuming a particular default function signature. To get a warning about this, compile with the -Wall flag:
gcc -Wall -c phaseOne.c
this will give you a warning of the form:
phaseOne.c:2: warning: implicit declaration of function 'your func here'
Unless you have good reason, you should always compile with the -Wall flag, and probably other warning flags too.
Undefined functions are not automatically an error; the compiler takes them to have a default prototype, then when your code is finally linked if there is something there of the right name it will be used. However, if the default prototype isn't what your function actually has, its arguments will be set up wrongly (think hammering a square peg into a round hole); the consequences will be unpredictable.
In most cases you should be telling the compiler to treat such cases as an error. For example, on gcc, add -Wall -Werror to every compile line, or add them to the CFLAGS in a typical Makefile.
I just wrote a sample program in two separate files:
a1.c
#include <stdio.h>
int main() {
// using the external object
testing();
return 0;
}
which calls a function which exists in a2.c
void testing() {
printf("temp\n");
}
and then all I did was compile it using the following command:
$ gcc a1.c a2.c -o a1
and it Worked
Actually, when you compiled your file did you include the other C file (lineParser.c) in the compilation step to be parsed along with your ParseOne.c file?
In that case, what I understand is that, gcc would actually parse all the symbols in the associated C files (or obj files) and replace them appropriately in the final object file to be created.
Hope this helps.

Resources