I have project like this:
When I try to compile this,get error:undefined reference to `Foo::Foo()'
I have compiled gtest 1.7 and gmock 1.7,and copied all libg* to /user/lib,and my build setting like this:
and project paths and symbols like this:
Update:
got a new error
undefined reference is a linker error, indicating the linker could not find implementation for the specific method.
try to add a implementation for you foo constructor in foo.h
Change:
virtual ~Foo();
Foo();
bool foo(void) { return true; }
To
virtual ~Foo();
Foo() {} //add default implementation
bool foo(void) { return true; }
Related
Below is simple program where i have configured VSCode on Azure VM with all applicable extensions to run c program
But when i try to compile i am getting below error
Any hints or missing extensions will assist... if i define func() at top prior main() it works but would be interested to know how do i make it work the way i am trying too ..any hints will be appreciated..
Place your entire void meow(void) function definition:
void meow(void) {
printf("meow\n");
}
outside of the main() function definition brackets { } like so:
int main(void) {
...
...
}
void meow(void) {
printf("meow\n");
}
Reference class
class commandsListClass
{
public:
std::string name;
std::string description;
std::vector<std::string> commands;
columnHeaders headersRequired;
void (*function)(System::Object ^ );
std::string recoveryFileHeader;
void reset()
{
name = "";
description = "";
commands.clear();
headersRequired.reset();
recoveryFileHeader = "";
function = dummyFunc; // dummyFunc uses the same members as the intended - this is to ensure it is defined. DummyFunc is empty, returns void etc
}
commandsListClass()
{
reset();
}
};
Currently, if I run the below code, the compiler crashes
// This crashes the compiler
System::Threading::ThreadPool::QueueUserWorkItem(gcnew System::Threading::WaitCallback(global::commandsList[index].function ), ti);
1>------ Build started: Project: MyProject, Configuration: Release x64 ------
1> project.cpp
1>c:\users\guy\documents\visual studio 2012\projects\MyProject\MyProject\Form1.h(807): fatal error C1001: An internal error has occurred in the compiler.
1> (compiler file 'msc1.cpp', line 1443)
1> To work around this problem, try simplifying or changing the program near the locations listed above.
1> Please choose the Technical Support command on the Visual C++
1> Help menu, or open the Technical Support help file for more information
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
If I declare a member inside the same function as I am making the call, and set it to the global::commandsList[index].function, it compiles and runs correctly
// This runs correctly
void (*func)(System::Object ^);
func = global::commandsList[index].function;
System::Threading::ThreadPool::QueueUserWorkItem(gcnew System::Threading::WaitCallback(func ), ti);
global::commandsList is a vector of type commandsListClass
Any ideas? Browsing Google and SO suggest changing the compiler to not optimize, which I've tried with no success. The code is written in such a way that:
That point in the code cannot be reached if index does not point to a valid member of the global::commandsList vector
The function variable is guaranteed to be set, either to the dummyFunc on creation, or the correct (requested) function as set elsewhere in the code.
Any help would be greatly appreciated.
Edit 1: This is using Visual Studio 2012, Windows 7 x64
Here's a simplified repo:
public delegate void MyDel(Object^);
void g(Object^) {}
struct A {
static void(*fs)(Object^);
void(*f)(Object^);
gcroot<MyDel^> del;
};
void(*fg)(Object^);
void h()
{
void (*f)(Object^);
A a;
gcnew MyDel(f);
gcnew MyDel(fg);
gcnew MyDel(a.fs);
a.del = gcnew MyDel(g);
//gcnew MyDel(a.f); // this line fails
// work around
f = a.f;
gcnew MyDel(f);
}
Only the non-static member variable fails. Seems like a compiler bug. Work around it by using a local intermediate.
Or better is Lucas's suggestion to use gcroot.
This is the error code i get when i attempt to build for testing, how do i find the cause for this error. I had duplicted 2 files in xcode and made subtle changes to copy in order to make a second screen.
ld: duplicate symbol _calculate_string in /Users/Lucky3kj/Library/Developer/Xcode /DerivedData/MiniCalculator-ebxkovztnlrphaahncircdyuwjgc/Build/Intermediates/MiniCalculator.build/Debug-iphoneos/PipeFitter.build/Objects-normal/armv7/RollingOffsetLeftViewController.o and /Users/Lucky3kj/Library/Developer/Xcode/DerivedData/MiniCalculator-ebxkovztnlrphaahncircdyuwjgc/Build/Intermediates/MiniCalculator.build/Debug-iphoneos/PipeFitter.build/Objects-normal/armv7/RollingOffsetAnyAngleViewController.o for architecture armv7
Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang failed with exit code 1
Basically, this is an error that stems from C.
If, in one .c file I have the following:
void myFunction(int myArg)
{
printf("%i", myArg);
}
And in another file I have this function:
void myFunction(int myArg)
{
printf("MyArg is: %i", myArg);
}
When the compiler links your project, and you call
myFunction(10);
The compiler does not know which version of your method to call, so the solution is one of the following:
1) Define the method once, and include only the prototype of the function. Example:
// instead of implementing myFunction here, we do this:
void myFunction(int myArg);
// and implement myFunction in another file.
-(void) viewDidLoad {
myFunction(10);
}
2) Define the method twice, but add the static qualifier to it, which tells the linker that this is the only file that can use this function.
// FileOne.c
static void myFunction(int myArg)
{
printf("myArg is: %i", myArg);
}
// FileTwo.c
static void myFunction(int myArg)
{
printf("%i", myArg);
}
Honestly, for simplicity, I would recommend just using the static qualifier, but thats just my preference when It comes to these matters.
This error generally come where you have done a circular reference or making two class files with same name.
If I missed the prototype, XCode (LLVM) prompt me for error
no previous prototype for function for exceptionHandler
But why they are needed in my code below?
void exceptionHandler(NSException * exception); // Why this Line is needed?
void exceptionHandler(NSException * exception)
{
// ....
}
#implementation AppDelegate
- (void) applicationDidFinishLaunching:(UIApplication *)application
{
NSSetUncaughtExceptionHandler(&exceptionHandler);
...
From the GCC manual:
-Wmissing-prototypes (C and Objective-C only)
Warn if a global function is defined without a previous prototype declaration. This warning is issued even if the definition itself provides a prototype. The aim is to detect global functions that fail to be declared in header files.
Clang borrowed this option for GCC compatibility, and because it's useful (I would presume this of the Clang devs).
The option exists so you can prevent yourself from making a common mistake which may be easily avoided. It's nice to be explicit about visibility/linkage for clarity/intent's sake.
In short, you've asked the compiler to tell you when an unqualified definition does not match a declaration by enabling this option. You should either qualify that as extern and make it usable to others (e.g. put it in a header), or declare it static. If using C++ inline is also an option.
Of course, implicit visibility is well known, but I typically find the option useful in these scenarios:
1) I made a typo:
// file.h
extern void MONExceptionHandler(NSException * exception);
and
// file.m
void MONExceptionhandler(NSException * exception) {
…
2) I should be explicit about the symbol's visibility:
// file.m
static void MONExceptionHandler(NSException * exception) {
…
3) I forgot to #include the header which declared the function:
// file.h
extern void MONExceptionHandler(NSException * exception);
Warning:
// file.m
void MONExceptionHandler(NSException * exception) {
…
No Warning:
// file.m
#include "file.h"
void MONExceptionHandler(NSException * exception) {
…
So there's the rationale, history, and some examples - again, -Wmissing-prototypes is an option. If you trust yourself to work with it disabled, then do so. My preference is to be explicit, and to let programs detect potential and actual issues so I don't have to do it manually.
If you're declaring a function only for use within this file, prefix the declaration with the static keyword and the warning will go away. As it is, you're declaring a global function; theoretically it could be called from anywhere within your app. But as you've given it no prototype, nobody else could call it.
So the warning, as I understand it, is trying to make you clarify your intentions between static functions and global functions, and discourage you from declaring a global function when you meant to declare only a static one.
I think this is most useful for C++ code. For example I have header
class MyClass {
public:
void hello();
};
and .cpp file
void hello() {
cout << "hello";
}
And you will see the warning because there are no prototype for function void hello(). In case the correct implementation should be
void MyClass::hello() {
cout << "hello";
}
So this warning make sure you are implementing the function that you are aware of (not miss typed a name or different argument format).
That warning is alerting that you can't call your method from another method that is written above. In C, the order of the declaration/implementation minds a lot and gives the difference between something that you can access or you can't.
I recently added some openfeint code to my classes and changed them to .mm
All of a sudden I get errors that duplicate symbols are found in the object files when building.
ld: duplicate symbol _audioPlayer in blah blah /Objects-normal/i386/Stage2.o and /Users/blah blah .build/Debug-iphonesimulator/blah.build/Objects-normal/i386/Stage1.o
Why is it suddenly causing this error? What exactly is the error?
I have variables with the same name in different classes, it should be a problem?
Thanks
You're probably declaring two variables with the same name in global scope (not inside interfaces), and the linker is complaining about that.
This error can also occur if you import a .m file instead of .h.
#import "SomeClass.m"
The short answer is that you can suppress this error with a command line argument to gcc:
-Wl,--allow-multiple-definition
If you implement your method like below in .mm file, duplicate symbol errorwill occur.
#import <Foundation/Foundation.h>
class CppTestOne
{
public:
void Test();
// {
// NSLog(#"Hello C Plus Plus");
// }
};
void CppTestOne::Test()
{
NSLog(#"Hello C Plus Plus");
}
then you can implement your method by
#import <Foundation/Foundation.h>
class CppTestOne
{
public:
void Test()
{
NSLog(#"Hello C Plus Plus");
}
};
//void CppTestOne::Test()
//{
// NSLog(#"Hello C Plus Plus");
//}
more details for this error not clear