How can I debug the operator.c file using gdb - gdb

I have divided my c code in three parts main.c operator.c and header.h I complie but but not able to debug the operator.c can some one help with this

Related

Having difficulties compiling simple c++ program on the command line with multiple files; maybe a linker error?

Sorry for the simple question. I am attempting to learn more c++ at a fundamental level. I have always used VS in the past, and I am trying to learn the command line and compile, navigate, etc. with it.
I started with "hello world" and was able to compile it with gcc/clang, then run it with the expected results.
I then slightly reworked this and made a new header/cpp file to do the output part of hello world, and then call that from the main function, described below:
main.cpp:
#include "MyClass.h"
int main(){
foo();
return 0;
}
MyClass.h
#pragma once
void foo();
MyClass.cpp
#include "MyClass.h"
#include <iostream>
void foo(){
std::cout << "Hello World\n";
}
I then have tried to compile with gcc and clang as follows:
clang -Wall -g main.cpp MyClass.cpp
I have tried the same with GCC, and have also tried various invocations of this, such as using -c:
clang -Wall -g -c main.cpp
clang -Wall -g -c MyClass.cpp
Each and every time, I get an error
λ clang -Wall -g MyClass.cpp main.cpp
main.cpp:13:1: error: use of undeclared identifier 'foo'
foo();
^
1 error generated.
I get this same error whether using gcc or clang.
I also tried from scratch on my laptop, to see if there was some more global issue, but I still get the same problem.
I have also tried on the basic Windows command line as well.
Other areas on StackOverflow demonstrate simple ways of compiling multiple files from the command line, and I have tried as they show, but still get errors.
I also know that "make" is something I need to learn as well, however, I just want to make sure I understand what my make file is doing before I dive into that.
I feel like it must be something trivial that I just cannot figure out.
Thank you to Andreas for the suggestion of looking at the preprocessor output. And thank you to everyone for the suggestions.
The pre-processor output did not make sense to what I was compiling.
I was using VSCode, in this case, as a text editor, making brand new files in my folder after launching it from the command line. I thought the files I created in VSCode directly into the folder (named main.cpp, for example), would produce a regular text file. However, for some reason, it did not.
Essentially, I recreated the above program in notepad and was easily able to compile it using the commands I used above. I guess VSCode may not be perfect for me as a pure text editor or I should figure out if there are settings to change to accomplish my goal.
Thank you all again for your time and consideration.
Use extern on your function. Also make sure you're compiling with c++ and not c; i.e. g++.
MyClass.h
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
extern void foo();
#ifdef __cplusplus
}
#endif

How can I isolate the compiled logic of a C++ program from the rest of the file header data?

I want to observe the difference in op code binary output of compilation between two versions of a very basic C++ program. For example, 2 + 2 = ?, with no libraries called. I expected the compiled output to be a tiny file of binary op codes with a few small headers, being new to compiled programs, but there are large headers.
simple.cpp
int main()
{
unsigned int a = 2;
unsigned int b = 2;
unsigned int c = a + b;
}
compiler:
g++ -std=c++0x simple.cpp -o simple
Is there a format that I can export to that doesn't contain headers, just op code binary that we instruct the machine to execute? If not, what bytes or location in the resulting file can I look for to isolate the relevant logic from the program?
I need the machine code, not assembly, since my project is the analysis of differently obfuscated versions of a source file to attempt recognizing one based on the other. A complex subject with questionable feasibility, but nevertheless that's why I'm asking to isolate the machine code and not just the assembly - to test analysis against the true machine code outputs.
I tried googling the header structure but can't seem to find much info.
Seeing ld(1): GNU linker - Linux man page, you will find that you can use --oformat=output-format option to specify output format.
binary is a format that don't have headers.
Then, seeing gcc(1): GNU project C/C++ compiler - Linux man page, you will find that you can use -Wl option to pass options to the linker.
-nostdlib option is also useful to avoid extra things added.
Combining these, you can try this command:
g++ -std=c++0x simple.cpp -nostdlib -Wl,--oformat=binary -o simple

Visual Studio C++ compiler tries to compile CUDA C/C++ files

I am trying to make a project I've been working on use CUDA.
At the moment it has four build configs, two (release and debug) which define a compiler symbol so it compiles with CUDA, and two (release and debug) which instead directs it to CPU code.
This is a short version of main.cpp:
#ifdef CUDA
#include "CUDACode.cu"
#else
#include "CPUCode.h"
#endif
int main() {
functionDefinedinBothHeaders(params);
}
but for some reason NVCC runs and compiles it fine but then the C++ compiler trys to compile it and that causes many errors that I've managed to circumvent using #ifdef __NVCC__ statements but now I have an issue where main.cpp has to use something from a header so I put is outside the #ifdef __NVCC__ statements and now I get linker error as they are defined twice as NVCC compiles it and the C++ compile does as well
error LNK2005: "class boost::random::mersenne_twister_engine<unsigned int,32,351,175,19,3433795303,11,4294967295,7,834054912,15,4293197824,17,1812433253> generator" (?generator##3V?$mersenne_twister_engine#I$0CA#$0BFP#$0KP#$0BD#$0MMKLIOOH#$0L#$0PPPPPPPP#$06$0DBLGKLAA#$0P#$0PPOFAAAA#$0BB#$0GMAHIJGF##random#boost##A) already defined in CUDAStateCalc.cu.obj
the .cu files are set to CUDA C/C++ code. How do I stop it from doing that? and is there a better method than what I'm doing?
Robert Crovella answered my question in the comments, what I needed to do was have a header function that was included in main.cpp and CUDACode.cu and contained the function prototype for functionDefinedinBothHeaders but CUDACode.cu defined it

How to compile a C++ source code via C++

Can we write a program in c++ that compile a c++ source code using a compiler ?
for example we have a program that takes the file name and then compiles it:
Enter your C++ source code file name : cppSource.cpp
your program compiled.
output: cppSource.exe
Or:
Enter your C++ source code file name : cppSource.cpp
Sorry. There is no C++ Compiler in your computer.
I do not mean that we write a Compiler.
I mean write a program that compiles the cpp file using a compiler.
How can we access a compiler. and how to detect that in a computer a compiler is installed or not.
Use system function to call any executable, for example g++ compiler:
#include <stdlib.h>
int retval = system("g++ file.cpp");
Return value of system can be checked, and it will be the return value of the called executable if the shell was able to execute it. In this case, usually it will be 0 if a compiler exists and the code compiled successfully.
Alternatively (and also to prevent called program output from being displayed), for additional details, it would be possible to redirect the program output to a file and then open that file and parse it's content.
int retval = system("g++ file.cpp > output.txt");

Help: VS2005 Compile *.m file

all
I have test.m( matlab source code) file which implement A() function; and main.cpp file (will call A() ).
as you know ,we may do like the following steps:
use matlab to compile test.m (mcc -) ,will generate : test.dll, test.ctf,test.h .
copy the test.dll and test.ctf ,test.h file to VS2005 project. in main.cpp, call A() in test.dll.
But,when i release the programe,i will also pack the test.dll together.
And another way, can i using the VS2005 to compile both test.m and main.cpp which will only generate main.dll,main.ctf,main.h..( i will only release main.dll,main.ctf,main.h,).
this means, i compile the test.m into main.cpp.
And i have tried this way, in VS2005 ---> Build Events--> pre-Build Event-->command line: mcc C -w lib:test test.m
and it will generate the mid-file test.ctf(only test.ctf,no test.dll).But i don't know how to compile test.ctf into main.cpp ?
could anyone help me ?
thanks.
You can do it the other way around and add your main.cpp to the matlab build process: I don't know the exact syntax, but you can add your main.cpp to mcc/mbuild, and it will add it to the dll for you. When using deploytool in gui mode, just drag c/c++ files to the resources area and they get compiled into the dll. So you'll have one dll only containing both m code and your own c++ code.
Another option, using the above strategy: first try the above, and look at the output of deploytool: it will show you the commands used. First it invokes mcc, then mbuild which in turn calls cl (the MS compiler). Use the exact command used to invoke mcc as a pre-build event, and then add that output files to cl in the same way mbuild does it (you can also see in the output how it does that). This way you can use VS to build a single dll anyway, just mimick what the matlab build process does.
Still I'm not sure how this is beneficial over distributing the two seperately. Also don't forget you have to distribute the entire MCR with it else your clients won't be able to run any code using the dll.