Finding a Variable/function across multiple files (Programmed in C) - header-files

A piece of source code which was not written by me, my aim is to find a problem with this source code. I've narrowed it down to 20 lines of codes.
In those 20 lines of codes a number of variables and functions are used.
There are upwards of 20 header files and similar .c files that are used to compile this code. I don't want to manually search through all these files.
Is there a way I can find where the function or variable is created and track it from there?
P.S. I'm using notepad++
To build this program I would need my board which I don't have access to, currently (Clarification on the build: I need the board containing the intel-microprocessor to compile and test the code)

You might try running ctags across the files and looking through the human readable cross-reference file to find where various variables and functions are defined.

Notepad++ v6.9.2 has a "Find in Files ..." (Ctrl + Shift + F) function.
Try that.

Related

How to Obfuscate Unrealscript code (compiled into .u files), so that Decompilers like UTPT and WOTgreal aren't able to decompile/crash?

I'm trying to find a way to make Decompilers (like UTPT, WOTgreal) essentially fail.
The files are .uc, written in Unrealscript, compiled into a .u file
Successful Example for UTPT:
https://i.imgur.com/kWBYi6U.png
I've tried simply naming the .uc files with 64 characters, which made the decompilers unable to decompile, but I ran into some other problem because of it, so I can not use that.
There's actually a way to do this with with a ucc commandlet called Editor.StripSourceCommandlet (https://wiki.beyondunreal.com/Legacy:Commandlet).

Dev-C++ Save source files with similar names?

I'm new to C++ and I'm using Dev-C++ compiler to learn my coding but I'm having some trouble saving the source files. If I save a source files with similar names to the first one, it always executes the first one whenever I compile and run it. For example, if I name the first source file Hello World.cpp and the second Hello World Anything.cpp it will always execute the code from the first source file. I either have to quit the program and create a new source file for the second or rename the file to something else like HW Anything.cpp. How do I save the files with similar names? Thanks!
Avoid using spaces in filenames/folders when dealing with Dev-CPP. instead, use underscores _ as a subsitute for spaces or CapitalWords.

Multiple Files to a project in c++

I have been writing code for a little while now and I recently found out how to create classes in different files and include them in main, along with more cpp files including the definitions of those classes. I was wondering when this is really needed, my code isnt normally that long. Should I use this now as a beginning when my code is only a few hundred lines or less or are the multiple files used with alot more code. In cases like this with such a short code I could probably find it easier just to stick to the main cpp. Any thoughts on this?
Bad habits are easy to learn and hard to unlearn. Why not do it the right way from the beginning?
I advise for the sake of organization that you should create a new set of files - .h and .cpp - for each new functionality you want to add. That doesn't mean create a new file for ever class you want to have. As an example, you can define a Sound class and a SoundManager class in the same header if you want. It's easier to find and edit a faulty piece of code when it isn't all jumbled up with other code in a catch-all file.
When you get to multi-thousand or even multi-million line projects (which I'm sure you will sooner or later), you will obviously be in for a world of pain if you shove it all into one file. Like David Schwartz said, it's better learn it correctly the first time than to relearn it correctly the second time.
If you have different parts of your code in different files, then you can open each file in a new editor window and view them side by side instead of having to jump up and down in the same file
Yeah, I know that some editors have a "split window" mode, but those don't play nicely with multiple monitors, etc, etc

How to find all global variables in C++ source code, DLL or any file created by the VC++ compiler?

I'm making my application thread-safe. One of the steps is to synchronize access or eliminate usages of global variables. I'm using Visual Studio. I can't find any good way to find all global variables in my codebase. It's impossible to create a good text search pattern and I can't find any helpful tool. Do you guys know any good way to do that? It could be a source code analysis tool or a binary file analyzer.
This could help:
Open the project in visual studio.
Open 'Class View' of the project
Under the project title, you will find 'Global Functions and Variable'.
I have checked this with Visual Studio 2010 and above.
Edit: As suggested by Ajay in comments, you could also categorize items in groups. For grouping items:
In class view, right click on project title
Select `Group By Object/Member Type'
Select the required tree like variables or structures or enums etc.
One option might be letting the linker generate a map file (/MAP in Visual Studio).
You will get a .map file for each binary with two sections:
A table of segments
Start Length Name Class
0001:00000000 00010000H .textbss DATA
0002:00000000 000034b4H .text CODE
0003:00000000 00000104H .CRT$XCA DATA
0003:00000104 00000104H .CRT$XCAA DATA
0003:00000208 00000104H .CRT$XCZ DATA
0003:0000030c 00000104H .CRT$XIA DATA
...
A list of symbols (functions and data)
Address Publics by Value Rva+Base Lib:Object
0000:00000000 ___safe_se_handler_count 00000000 <absolute>
0000:00000000 ___safe_se_handler_table 00000000 <absolute>
0000:00000000 ___ImageBase 00400000 <linker-defined>
0001:00000000 __enc$textbss$begin 00401000 <linker-defined>
0001:00010000 __enc$textbss$end 00411000 <linker-defined>
0002:000003a0 _wmain 004113a0 f console4.obj
...
You can tell apart the functions from variables by the "CODE" / "DATA" designaiton in the segment list.
Advantage: You will get all symbols, even those in libraries, that were not removed by the Linker.
Disadvanatge: You will get all symbols, even those in libraries, that were not removed by the Linker. I don't know of any tool that does the code/data separation automatically.
I know the http://code.google.com/p/data-race-test/wiki/ThreadSanitizer program (product of google) which can work in Windows and on compiled code. It is dynamic instrumentation program (like valgrind or bit like qemu/virtualbox), which add some checks to memory accesses. It will try to find some threading problems. You can just run your program under control of threadsanitizer. There will be slowdown from dynamic translation and from instrumentation code (up to 20x-50x times slower). But Some problems will be detected automatically.
It also allows you to annotate some custom synchronization functions in source code.
Wiki of program has links to other thread-race detectors: http://code.google.com/p/data-race-test/wiki/RaceDetectionLinks
cppclean is a static analysis tool that can help you. From the documentation:
cppclean finds global/static data that are potential problems when using threads.
A simple example with a static local variable and a global variable follows.
./example.h:
void foo();
./example.cpp:
#include "example.h"
int globalVar = 42;
void foo(){
static int localStatic = 0;
localStatic++;
}
Open a terminal and run cppclean as follows:
$ cppclean --include-path . example.cpp
example.cpp:3: static data 'globalVar'
example.cpp:6: static data 'localStatic'
Unfortunately, cppclean has some parsing issues and bugs. However, these issues are pretty rare, and affected below a percent of all code I've tested.
You can try CppDepend by using its code query language
from f in Fields where f.IsGlobal select f
Maybe dumpbin tool will help here. You can run it with /SYMBOLS key to display the COFF symbol table and look for External symbols - global variables should be in this list. DUMPBIN /SYMBOLS.
You all are making this too complicated.
1.
Copy the code of each of your files (one at a time separate from the others) to a string or a wide string or etc. and then parse out everything that is from "{" to "}" uninclusive. Save the result to an exterior file. After the first time, then append to that file.
2.
Even if you have 1,000 lines total of what is left after all that parsing, in that are all of your globals (depending upon how you created the globals). If you created them via a namespace, etc. then go back and parse for that. I doubt that most programmers will have 1,000 globals, but for some applications it might be what they use. If you do not have too many at that point then manually edit that text file of the results.
I have found that maybe 90+ % of the answers on this site are bloated with far too much complexity that just eats up cpu time and memory space. Keep it simple.
You might find it handy to have a globals.h file which you load early and keep most or all of you globals there. It looks like time to do a lot of clean up.

Is it possible to have identically named source files in one visual studio c++ project?

I'm working on a static library project for a c++ course I'm taking. The teacher insists that we define only one function per source file, grouping files/functions belonging to the same class in subdirectories for each class. This results in a structure like:
MyClass
\MyClass.cc (constructor)
\functionForMyClass.cc
\anotherFunctionForMyClass.cc
OtherClass
\OtherClass.cc (constructor)
Whether this is good practice or not is something I'd not like to discuss, since I'm simply obliged to organize my project in this manner.
I'm working in visual studio 2008, and somehow got strange link errors when using an identically named function (and thus filename) in two classes. This appears to be caused by the fact that visual studio puts all .obj files (one for each source file) in one intermediate directory, overwriting earlier generated object files when compiling identically named source files.
This could be solved by putting the object files in subdirectories based on the relative path of the input file. Visual studio allows one to configure the names of object files it generates and has macros to use in there, but there appears to be no macro for 'relative path of input file'.
So, is there some way to get this to work? If not, is using one project for each class the best work-around?
You are right, by default all object files are put into the same directory and their filenames are based on the source file name. The only solution I can think of is to change conflicting file's output file path in here:
Project Properties-C/C++-Output Files-Object File Name http://img37.imageshack.us/img37/3695/outputfile.png
PS. It sounds like the lecturer has a crappy (probably written by the lecturer himself) automatic code verifier that imposes this restriction. To get extra marks, offer to rewrite the parser so it works with normal/sane/non-weird projet layout.
Real answer:
Change
C/C++ => Output Files => Output File Name
to
$(IntDir)/%(RelativeDir)/
Every .obj file is going to be created in a sub folder so its not going to overwrite the previous on linking.
I can't think of any way to fudge the project settings to get VStudio to automatically split out the intermediate files into separate folders.
You have a few chances -
Build the class name into each file name. Most IDE's display just the file name in the tab view so if you do have several methods in different classes with the same name, its going to be difficult to tell them apart if the file name does not include the class name along with the method name. Which is really why I think your teachers advice is madness. I have not seen any programming style guide advocating that approach. Additionally it goes directly against the way various tools work - if you use Visual Studio to create a class, it creates one cpp file and one header, and automatically appends each new function to the single cpp file.
You could create a static library per class. When linking in static libs the obj files are all packaged up inside the .lib so conflicts are no longer a problem.
Switch comp-sci courses to one thats not being taught by a nut job. Seriously, this guy is completely out of touch with industry best practices and is trying to impose their own weird ideas on their students: Ideas that are going to have to be unlearnt the moment they leave the teaching environment.
You can also change output file name per file in its properties. Just make sure you use different names.
Can you use the class name in the filename to disambiguate? I'm thinking that you might have
MyClass
\MyClass.cc (constructor)
\function1_MyClass.cc
\function2_MyClass.cc
That would mean that every file would have a unique-enough name to defeat the problem. Is that an acceptable strategy?
You could probably arrange the properties of the project to put the object files into a folder which is below the folder of each source file. Once the project has this property, then every source file should inherit this property. (But if you've done experiments like Igor has suggested, then you may need to go through the properties as reset them back to the parent).
Having looked at the help files, I think you should go to project properties/C C++/Outpuf Files/Object File Name: and enter $(InputDir) (no trailing backslash). Every source file should then inherit this property and your .obj files should be separated.
You may need to do a Clean Solution before you make any changes.
Renaming the object files will work, but it's going to be a pain, and it will slow your compile/link cycle down. I've never figured out why, but it seems to confuse Visual Studio if the object files don't have the default names.
You could prefix the funciton name with the class name; e.g. myclass-ctor.cc, myclass-function1.cc etc.
You could have one .cc file per class which #includes the individual function files. In this case you'll need to prevent the #included files from being compiled seperately (either rename their extension or set Properties->Exclude From Build to 'Yes').
Out of curiosity, where does your teacher want you to put free functions e.g. local helper functions that might normally belong in an anonymous namespace?
If not, is using one project for each class the best work-around?
Not a good idea - apart from the fact that you won't end up with a single static library (without even more jiggery pokery), your link times are likely to increase and it will hide a
lot of pertinent info from the optimizer.
On another note; If the course is actually about C++ not OO programming, do what you need to pass but take your teacher's advice with a pinch of salt.
You don't have to put them in different translation units... why not put each function in a .h and include them all in one .cc per class? That will very likely give better output from the compiler.
I'd be asking why the teacher is insisting on this odd structure, too, the reasoning behind it should be explained. I know you didn't ask that of us, so that's all I'll say.
In Visual Studio 2010, I set
Properties -> C/C++ -> Output Files -> Output File Name
to
V:\%(Directory)$(PlatformName)_$(ConfigurationName)_%(Filename).obj
for OBJ files to end up next to the sources assuming the project lies on drive V (no idea whether there is a macro for it, yet).
By the way: $(InputDir) refers to the solution/project directory and will cause the same problem in another directory.