Xcode beginner needs help starting debugging - c++

I'm a novice C++ programmer and I only know how to program in visual studio, but my PC crashed and I only have mac Xcode available.
I wrote a .cpp like in visual studio, but when I had it build, it gave me this error..
ld: duplicate symbol _main in /Users/karen/Desktop/BD/build/BD.build/Debug/BD.build/Objects-normal/x86_64/bd1.o and /Users/karen/Desktop/BD/build/BD.build/Debug/BD.build/Objects-normal/x86_64/main.o
Anyone know what to do?

This error is telling you that you have defined "main" in two separate files: (probably "bd1.cpp" and "main.cpp"). If you didn't create the file "main.cpp" (or "main.cc"); it's possible that XCode created a sample "main" for you when you set up the project.
A program can have only one function named "main", so you need to get rid of one of them...

Its a linker error, so you can't really "debug" this.
Can we see some code? bd1 and main have a duplicate symbol - so u use maybe same variable/function names without namespaces? Its also possible, that you need to "cleanup" before you try to build (old symbols in object files) - but its like guessing without seeing your code...

Either bd1.cpp or main.cpp should go, as they both have a main function. There can only be one main function in a C/C++ program. Most likely main.cpp was automatically created for you when you created a new Xcode project for existing codebase. So search for main.cpp/main.c in your project and remove it.

Related

How to Debug a COM DLL

I made changes to a function in a COM DLL. I've been unable to figure out how to debug the changes I made.
Background:
I recently inherited a Visual Studio 2012 C++ project that dates back many
years. I'm running Win7 Professional 64-bit.
The top-level design of the project is this:
The code that does most of the work is encapsulated in COM DLLs.
For each DLL there's a separate wrapper function that calls:
(1) CoInitialize
(2) CoCreateInstance
(3) CoUninitialize
There's a main program that presents a dialog to allow a user to select an option. Based on the selected option, the main program calls the appropriate wrapper function, which then runs the code in the corresponding COM DLL.
Problem Details:
(1) I've been unable to step through the code in the Visual Studio debugger.
(Trying to run in the debugger produces the error "Unable to start program
", where the named DLL is different from the modified one.)
(2) I put "fprintf(stderr, ...)" calls in the modified DLL code, but didn't get any output from the "fprintf" calls. (I do see output from "fprintf" calls I added to the wrapper function that invokes the DLL.)
I also tried opening a temporary debug file using "fopen", writing debug statements to the file, then fflush, and fclose. Also no output.
(3) I noticed a post (Calling fprintf from dynamic library (c++)) that suggested that, although "fprintf(stderr, ...)" should work, it would be better to implement a callback to a debug function in the main program. I attempted to do that.
The changes compile, but the linker reports an undefined reference to the name of the function in the DLL that was intended to allow the main program
to pass in a pointer to a callback function.
I'm confused by the undefined reference, because the modified DLL has a different exported function that the linker is able to resolve.
Specifically:
__declspec(dllexport) void SetLogFunc(LogFunc LogFuncPtr) [The new function.]
__declspec(dllexport) BOOL DoRosSum(SRosSumData* pRosSumData) [The existing function.]
I used the "ack" utility to search the entire codebase for the project,
including Visual Studio project files and binary files, looking for
references to "DoRosSum", and can't find any place where there's a reference
to "DoRosSum", but not also a reference to "SetLogFunc".
("SetLogFunc" is listed by "dumpbin" as an exported function.)
I should also mention that I reverted all my changes except for:
the "SetLogFunc" function in the COM DLL,
the callback debug function in the main program, and
the call to ""SetLogFunc"" in the main program.
so I don't think the problem I'm having getting debug output, or running in the VS2012 debugger, is related to the modification I originally made to the DLL code.
Apologies for the long post. In recent years I've mostly been working in C#, or working on linux systems. I have no experience with COM DLLs, so I may be
missing something simple.
If anyone has any thoughts on how to proceed I would appreciate it.
For the first question,
https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-debug-from-a-dll-project?view=vs-2017
I think you can check if you have done this yet. Usually, if you haven't set up command correctly in project property/debugging, running debugger in VC always show you the message:
Unable to start program '......\your DLL'.
Since DLL need to be execute with an executable(.exe).
I noticed that you said the error message 'the named DLL is different from the modified one'.
Your main program will run with multiple DLL right? I think you also need to make sure you put other DLL(.dll) file in the folder of your main program application.
For undefined reference issue,
If you can compile the functions in DLL sides, it means the logic here is ok. However, linker report undefined error then it should be some issues for exporting this function to main program side.
I suppose you to first check in main program side to see if you include the correct header file.
Like right clicking the function name in main program "Go to Definition" will show you the header file. You can then see if the header file is the correct one.
Then you can check if you export functions correctly. You need to make sure main program link to the new compiled Object File Library(.lib). Every time you compile your DLL project you need to make sure main program link to the new compiled .lib and .dll and include the new header file.
Hope these helps.

(LINUX)Porting from Qt4 to QT5 undefined symbol : _Zn9Qwidget11stylechangeER6QStyle

I ported my application from qt 4.8.0 to qt 5.5.1
It compiled without any error but at run time the application crashes and get the message undefined symbol : _Zn9Qwidget11stylechangeER6QStyle in one of my .so file linked dynamically.
When searched couldn't find any function styleChange() in whole project source code.
Cleaned project, deleted all intermediate files, all object files, .so file but same result.
It seemed it is a qt4.8.0 function but could'nt find and hence not able to debug.
Any help would be appreciated.
When searched couldn't find any function styleChange() in whole project source code.
Note that in the mangled name 'c' is in lowercase: stylechange. Have you tried to search for this method?

Undefined reference error to function in other file [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed last year.
I have a game program and I am getting VERY frustrated. Everything was running fine, and I decided to clean up my program by making separate files for each set of functions. The code is very long with multiple files, but heres the basic idea:
Im on Windows XP using the Code::Blocks IDE
In my entity.h Ive declared all of my functions and variables for that class. In my entity.cpp Ive included it, as well as in all my other files. But Im still getting a huge list of errors that tell me I have an undefined reference to all of the methods in entity.h as well as all my other header files. For example, I have a function call print() to make it easier to print out things, and thats the first method I call from the entity.h file. I get this error:
Heres the code for print():
void print(string f) {
cout<<f<<endl;
}
How Im calling it:
void Player::win(){
entity e;
e.print("You have defeated the orc");
}
The error:
In function 'ZN6Player3winEv':
undefined reference to 'entity::print(std::string)'
And yes, I do have an object of entity.
Undefined reference errors happen at link time (as opposed to compile time). Your code seems to be compiling correctly, so your headers are probably correct. But you're not linking everything together to make the executable. You didn't mention the platform, compiler, or build system, so I can't tell you exactly how to fix it, but in general the compiler generates a *.o file for each *.cpp file (that's the compilation) and then links the *.o files together to create an executable. You need to make sure all your *.o files are being linked together (also, you might not be compiling one of your *.cpp files into a *.o file).
At a quick guess, you're not linking the entity.o file to the binary.
If you're using linux g++, then something like:
g++ -o binary entity.cpp game.cpp
should compile and link the cpp files to the binary.
For Code::Blocks to select the files that are within a specific build group, you need to right-click on the target application and select properties. This will pop-up the Project/target options. Go to the Build Targets tab. Towards the bottom-right of the page is a list of Build target files. You need to make sure that all the files that contribute to the executable are selected as part of your build target. This means that you need a check-box on the entity.cpp file. This ensures that the file that contains the code for entity::print is compiled and linked into the target executable. Please note that the Build target files tab applies to the selected build target in the list at the left-hand side. You will need to ensure that the file is checked for all the build targets that it belongs to.
In general, when you're adding files in Code::Blocks, and where you see the check-boxes for the targets that the file should be added to you should make sure that all the targets that the new file belong to are checked. The default from the wizard is to leave all the check-boxes unchecked. In your case, as it's a simple project, you should make sure that it is checked for each target (typically it will start with debug and release as the two targets).
I had the same problem. For me the solution was :
When you add a file in your project, dont forget to click on "add to DEBUG" and "add to RELEASE", either the file will be in the project tree yes, but you will have error "undefined reference"
The problem is that originally you were defining a free function named print instead of a member function named print of class entity. You need to be in the scope of the class entity to define it as a member function which is what i did using scope resolution operator :: below.
/////vvvvvvvv
void entity::print(string f) { //note the scope resolution operator:: used here
cout<<f<<endl;
}

Duplicate symbol_main

I've been looking for solutions for this problem but them didn't work for me! I've been having a lot of problems compiling programs.
When I'm trying to execute a program in Xcode appears:
duplicate symbol _main in /Users/LauraChaparro/Library/Developer/Xcode/DerivedData/Busqueda-ercduihvfosqcoczkrgljkkmgxam/Build/Intermediates/Busqueda.build/Debug/Busqueda.build/Objects-normal/x86_64/Uno.o and /Users/LauraChaparro/Library/Developer/Xcode/DerivedData/Busqueda-ercduihvfosqcoczkrgljkkmgxam/Build/Intermediates/Busqueda.build/Debug/Busqueda.build/Objects-normal/x86_64/main.o for architecture x86_64
I don't understand why this happens! Is a C++ project... Or if you can recommend me another IDE I'll be really thankful!
It looks like you are defining a main in some of the code used to make the Uno.o object file. You can only have one main, so objects used to build libraries should not define a main. This should only be defined in the application itself.
This a linker error and it likely you would encounter it with other IDEs. You need to find where in Uno.o the main is defined and remove it.
The error you have there is a linker error. It tells you exactly what is wrong. The linker has found out that it can find the symbol *_main* defined more than one time looking through the object files. This makes it impossible for the linker to create an executable of your object files, as it have no way of telling which symbol you want to use. It continues to tell you where it found the two symbols.
Once in the Uno.o file (which I will assume come from compiling the Uno.cpp file)
another from the main.o file (from the main.cpp file)
This means, that somehow the linker can find the symbol for the main method in both files.
This could happen by:
Defining a main function both in the Uno.cpp and main.cpp
Having placed a non inline main definition in a header and including this both in the Uno.cpp file and the main.cpp file (or an include that includes another include and so on.)
As your request for another IDE I would like to remind you that neither compiler errors or linker errors are errors in the IDE, in an overwhelming number of cases the problem is that the user of the IDE has done something wrong.
This happened to me once, Make sure you include your .h file instead of your .c file in the file you don't expect your main function to be. I had my include statement having .c file and it produced the same issue .

Symbol Not Found, expected in Flat Namespace ObjC++

I've got probably what is a simple problem, but there's no informative errors or warnings during compile to alert me to what is going wrong.
I've got a Objective-C++ app that includes a C++ main and ObjC header files.
It builds fine, but when run, it gives this error message:
Dyld Error Message:
Symbol not found: _OBJC_CLASS_$_AppController
Referenced from: /Users/slate/Documents/osirixplugins/eqOsirix/build/Development/rcOsirix.app/Contents/MacOS/rcOsirix
Expected in: flat namespace
in /Users/slate/Documents/osirixplugins/eqOsirix/build/Development/rcOsirix.app/Contents/MacOS/rcOsirix
No amount of googling has resulted in a solution, and I'm sure I've just missed a compilation or build option somewhere.
"AppController.h" is included in the target (checked), and #import'd in the ObjC Class File.
Any help is greatly appreciated.
ObjC++ constantly gives me a headache.
Thanks,
-S!
Clearly the AppController class is missing. Is the AppController class defined in a framework of dynamic library? If so, when you run the app, does it know where to find the libraries/frameworks?
This is a linker issue, by the way. The header files are irrelevant. It's the .m or .mm files you need to look at.
Not sure if this is your issue, but I was having a similar problem with a C++ dll, which took me all day to debug. I haven't programmed in C++ for around 15 years, and while attempting to write a pure virtual function, I used the familiar syntax "virtual void f();" -- oops. In C++ it should be "virtual void f() == 0;" The latest version of gcc on mac OSX 10.9.2 happily compiles the code.
I think it's a perfectly legal forward declaration... however, not sure if C++ allows classes to span multiple files, so it seems like this should be flagged (as no implementation is ever supplied in the CXX file.) In any event, the symbol makes it into the object code, and the linker never complains, so you end up with the missing reference. Hope this helps someone.