Visual Studio 2010: strange linker error - visual-studio-2010

I have the following code in my project:
SomeCode.h :
#pragma once
#include "defines.h"
void Function1(int *param1, float *param2, int count);
void Function2(int *param1, float *param2);
void Function3(int *param1, float *param2);
SomeCode.cpp :
#include "SomeCode.h"
void Function1(int *param1, float *param2, int count)
{
//implementation
}
void Function2(int *param1, float *param2)
{
//implementation
}
void Function3(int *param1, float *param2)
{
//implementation
}
main.cpp:
#include "SomeCode.h"
int main()
{
// some Function1, Function2, Function3 usage
}
All the above files are in the same folder.
The solution compiles fine, but i get Function1, Function2, Function3 unresolved external errors. Cleaning and building again doesn't help. However a magical workaround to make it building successfully is the following:
1) comment all the lines in SomeCode.cpp
2) build the solution and get the same linker errors
3) uncomment the lines
4) build again
I encounter the same problem from time to time when I change something in my project (adding new files, making changes to existing once). However this happens not on a regular basis, so I can't tell precisely when the problem appear.
Any ideas what might cause these troubles?

Do you try to add a
#include "somecode.h
In somecode.cpp ?

Related

LNK2019 on function that does exist in an included *.cpp file

For Visual Studio 2019 on Windows 10 in a new project I'm getting an unexpected LNK2019 error.
I have a test function calling a function in another project where I've included the necessary *.cpp file into the test project so I can access the internals of a DLL for testing.
test.cpp:
#include "dllsubmodule.h"
void TestCountPagesInSomething()
{
// ... setup ...
int nPages;
nPages = CountPagesInSomething(iCurrentPos, hashFoundPages, hashPageCounts, sFoundData);
}
int main(int argc, char **argv)
{
TestCountPagesInSomething();
}
dllsubmodule.h
#pragma once
int CountPagesInSomething(
int iCurrentPos,
std::map<int, ERPPageInfo>& hashFoundPages,
std::map<std::string, int>& hashPageCounts,
const wxString& sFoundData
);
dllsubmodule.cpp
#include "stdafx.h"
#include "dllsubmodule.h"
int CountPagesInSomething(
int iCurrentPos,
std::map<int, ERPPageInfo>& hashFoundPages,
std::map<std::string, int>& hashPageCounts,
const wxString& sFoundData
)
{
// implementation code here...
}
For whatever reason the linker error seems to be complaining that CountPagesInSomething() doesn't exist. I suspect I'm having trouble with name mangling variations but I've no idea how to diagnose the problem.
Note: CountPagesInSomething() is internal to the DLL and not a DLL export.
Right now this is just a console project. I'm trying to get a basic printf() test working before I figure out what kind of unit test framework to use.

Visual Studio: Vector of Unique Pointers of Subclasses Crashes

In Visual Studio 2015, the code shown below crashes at the exit of foo(), with this error message: HEAP[NameOfExecutable.exe]: Invalid address specified to RtlValidateHeap( 00520000, 005332D4 )
Notes:
When I remove the "virtual" keyword from B::bob(), the program works perfectly fine.
The program runs correctly from a command-line (outside of Visual Studio), when compiled with "g++ -std=c++11".
Using std::move() within push_back() didn't change the output.
#include <vector>
#include <memory>
#include <iostream>
class A
{
int a;
};
class B : public A
{
int b;
virtual void bob() { };
};
void foo()
{
std::vector<std::unique_ptr<A>> test;
test.push_back(std::unique_ptr<B>(new B));
}
int main()
{
foo();
std::cout << "Reached the end\n";
std::cin.get();
}
What am I doing wrong? Thanks for any help!
If you debug the code, you'll notice that the exception arises at the exit point of foo() - when the destructor for test is called.
Both std::vector and std::unique_ptr manage their memory of course, so somewhere at this point B object that you just allocated will be destroyed.
The problem here is that you store it by an A pointer, so the A class destructor will be called.
To fix that, just introduce public virtual destructor to 'A':
class A
{
int a;
public:
virtual ~A() {}
};

Arduino 1.6.9/1.610 build fails with "'constexprint' does not name a type"

Supposedly Arduino's IDE > 1.6.2 has C++11 support.
I have just freshly downloaded and run version 1.6.9 on OSX (and as others have reported, this repros on Windows as well, with 1.6.9/1.6.10).
I cannot get this simple program to compile:
constexpr int get_five() { return 5; }
void setup() {
Serial.begin(9600);
Serial.println(get_five());
}
void loop() {
}
I receive this error when I try to build or upload:
sketch_jul25a:1: error: 'constexprint' does not name a type
constexpr int get_five() { return 5; }
^
exit status 1
'constexprint' does not name a type
I've looked at this question and answer, but it is supposedly no longer applicable in 1.6.9 version of the IDE that I am using - error: 'constexpr' does not name a type m- arduino ide
I have dug into the temporary files that are output by the IDE when building, and it seems it is trying to automatically generate headers for functions (I assume for multi-file sketch support), and does the wrong thing when it encounters constexpr:
#include <Arduino.h>
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
constexprint get_five(); // **** <- This looks to be the culprit
#line 3 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
void setup();
#line 9 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
void loop();
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
constexpr int get_five() { return 5; }
void setup() {
Serial.begin(9600);
Serial.println(get_five());
}
void loop() {
}
Is this a bug in the Arduino IDE? Is it unique to OSX? Is there a workaround that allows constexpr to work?
In googling I have found that some Arduino libraries are using constexpr, so I am assuming it could be made to work in some cases.
This is a known limitation of the arduino-builder.
Until it is fixed, you can add a prototype yourself above the function. This will prevent the IDE from incorrectly generating its own.
constexpr int get_five();
constexpr int get_five() { return 5; }
void setup() {
Serial.begin(9600);
Serial.println(get_five());
}
void loop() {
}

Seemingly erroneous "Undeclared Identifier" for header file

I have the following files:
Source.cpp
#include "SFML\Graphics.hpp"
#include "SpriteControl.h"
using namespace sf;
int main(int argc, char ** argv){...}
SpriteControl.cpp
#include "SFML\Graphics.hpp"
#include "SpriteControl.h"
using namespace sf;
SpriteControl::SpriteControl(){}
SpriteControl::SpriteControl(Texture textureIn){
texture = textureIn;}
SpriteControl::~SpriteControl(){}
void SpriteControl::setMoveable(bool in) {
isMoveable = in;}
void SpriteControl::setVelocity(float vx, float vy) {
if (isMoveable) {
velocity.x = vx;
velocity.y = vy;
}
}
Vector2f SpriteControl::getVelocity() {
return velocity;}
SpriteControl.h
#pragma once
class SpriteControl{
Texture texture;
Sprite sprite;
bool isMoveable;
Vector2f velocity;
public:
SpriteControl();
SpriteControl(Texture textureIn);
~SpriteControl();
Vector2f getVelocity();
void setMoveable(bool in);
void setVelocity(float vx, float vy);
};
When I try to build, I get undeclared identifier errors for "texture" and "velocity", as well as "overloaded member function not found" for my constructor, "left of .x must have class/struct/union", "getVelocity is not a member of SpriteControl", and so on. An error for almost every line of both the SpriteControl.cpp and SpriteControl.h files, none of which make any sense. Any thoughts?
Note: the SFML libraries have all be linked properly. Earlier, simpler versions of the program without any header files compiled and ran just fine.

Boost thread v1.53 segmentation fault

The following program produces a segmentation fault, although I don't see any undefined behaviour in the code. It has been compiled with GCC 4.7.3. Do you know the reason of the fault or a possible work-around? Also, it seems boost::future does not exist in v1.53 yet, so I should probably rely on boost::unique_future. I cannot upgrade to any version above > 1.53 and I really need the "make_ready_at_thread_exit()" feature.
#define BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>
namespace th = boost;
struct S {
th::packaged_task<void()> task;
th::unique_future<void> future;
void start();
void stop();
};
void S::start() {
task = th::packaged_task<void()>{ [this] () {}};
future = task.get_future();
task.make_ready_at_thread_exit();
}
void S::stop() {
future.wait();
}
int main() {
S s;
s.start();
s.stop();
}

Resources