Rotating LNK2005 / LNK2019 Errors in CPPUnitTest in C++ Visual Studio - c++

I've been trying to set up a CPPUnitTest to test a C++ project. I've came across an error where I've got two rotating errors depending on how I try to solve my problem.
I've got two projects in a solution in Visual Studio. One is for testing, one is for the project itself. I'm having these errors whilst trying to reference the project in the testing project.
If I do this, I get a LNK2019 (unresolved external symbol) error any time I try to construct an object or call a function:
#pragma once
#ifndef REFERENCE_H
#define REFERENCE_H
#include "../Stuff/Thing.h"
#include "../Stuff/OtherThing.h"
#endif
However, if I do this, I get a LNK2005 (test2.obj: blahblahlblah is already defined in test1.obj) error since two of the tests reference it:
#pragma once
#ifndef REFERENCE_H
#define REFERENCE_H
#include "../Stuff/Thing.cpp"
#include "../Stuff/OtherThing.cpp"
#endif
Deleting one of the tests fixes the problem with the latter (.cpp) but obviously that's not very good.
I think I may have missed a step somewhere along the way but I'm not sure what it is. I did add the "project" project as a dependency to the test.
Does anyone have the solution to this?

Related

VS 2019 C++ Missing symbols in pch.h

I've seen the related question regarding "_T()" not found. I have a console app. The "Debug" version of the console app compiles and runs fine. When I try to compile it as a "Release" version I first get hundreds of _T() Not Found. I tried adding #include <tchar.h> to pch.h which was completely ineffective. Based on what I found in the question about undefined _T() I read, I added the text
#ifdef _UNICODE
#define _T(x) L ## x
#else /* _UNICODE */
#define _T(x) x
#endif /* _UNICODE */
#include <tchar.h>
#include "framework.h"
to pch.h before #include "framework.h". The compiler noticed but now complained as follows:
Error C2039 '_tcscpy_s': is not a member of '`global namespace'' Anon C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\atlmfc\include\atlchecked.h 111
plus a few hundred other similar messages.
This is not the first time this has happened. Every project I've tried to build with VS2019 eventually has this happen to it. And, this happens compiling a completely pristine "pch.h" that I had not (initially) modified at all before this error occurred.
I've already tried deleting VS and reinstalling it. My OS is Windows 10 (64-bit)
The list of Include directories is the same for both the Debug and Release configurations.
I feel that this must be related to something in the way Visual Studio is configured, but mostly I need help fixing it. The compiler seems to be getting tangled up in its own header files. What can I do to analyze/fix this?
ADDENDUM:
Just since I started writing this question I have gone back to my project and tried the Debug version again. Now it is getting the same errors. And I made no changes since the previous time I built it earlier today.

"undefined reference to 'operator new(unsigned int)' and undefined reference to class functions (and constructor)

I'm attempting to write code for an ATxmega16E5 using Atmel Studio 7.
I've had a long search around the forums and cannot make head nor tail of the other suggestions that seem to be similar to my own issue.
(A lot of suggestions indicate to change compiling options, which I have no idea how to do this in Atmel Studio).
Basically, I've included my class GPP.h
BUT, when I try to instantiate a class in main.cpp it comes back as a series of undefined references to all functions including the constructor.
EDIT: Reduced the amount of included code to focus on the main issues at hand,
mainly, the files don't seem to be linking.
main.cpp :
//INCLUDES
#include "GPP.h"
//MAIN FUNCTION
int main()
{
GPP *gpp = new GPP();
//Turn on system power
gpp->setPowerPin();
gpp->screenOn();
// ...etc (just makes some function calls
// ...
while(1);
}
GPP.h:
#ifndef GPOWERPACK_H
#define GPOWERPACK_H
class GPP
{
public:
GPP();
//~GPP();
void setPowerPin();
void screenOn();
void SPI_Init();
void SPI_SendNext();
void displayLogo();
};
#endif
Any help would be fantastic to help grow my understanding of cpp.
I have coded in c and java previously.
Atmel Makefile is quite long, Could display sections on request?
For anyone using Atmel Studio for the first time.
And those who have simply forgotten the basics of makefiles.
I figured out a simple way to resolve my linker issue.
The error 'undefined reference' occurred because I had not included the files GPP.cpp and GPP.h in the project build.
The fix:
1) Open the "Solution Explorer"
2) Right click on your project, go to add -> existing item
3) select the files you are including/ files relevant to your project build and click
Apologies for the misguided question. Seems a simple mistake in hindsight

Global variables in C++ and CriticalSections

I have several global critical sections that need visibility across 3 or more classes defined in 3 or more .cpp files. They're defined in an h file as:
GlobalCS.h
#pragma once
#include "stdafx.h"
extern CRITICAL_SECTION g_cs1;
extern CRITICAL_SECTION g_cs2;
etc...
In a GlobalCS.cpp they are defined
#include "stdafx.h"
#include "GlobalCS.h"
CRITICAL_SECTION g_cs1;
CRITICAL_SECTION g_cs2;
Then in the cpp for the classes they need to work in they're included as
#include "stdafx.h"
#include "GlobalCS.h"
The linker is complaining giving unresolved external in the files where the critical sections are being used. I didn't expect this since the variables are defined as extern. What am I doing wrong or how do I get around this?
error LNK2001: unresolved external symbol "struct _RTL_CRITICAL_SECTION g_CS_SymbolStrPlotAccess" (?g_CS_SymbolStrPlotAccess##3U_RTL_CRITICAL_SECTION##A)
Not sure why you are getting the error. I tried the same thing in Visual Studio and in _tmain function I wrote the following:
int _tmain(int argc, _TCHAR* argv[])
{
//::g_cs1;
ZeroMemory(&g_cs1, sizeof(::g_cs1));
return 0;
}
And it built with no issues whatsoever.
Thank you all for your help. It always is helpful to have sanity checks. The issue was once again Visual Studio setup. Visual Studio will generate link errors if it doesn't like the way you have files added to your project. This is the second bug I've encountered that generated a link error based on the way the project was configured. If it's that important VS should either prevent you from modifying a project in a harmful way or provide a proper error message.
Anyway, the error is the same as this one:
LNK2019 Error under Visual Studio 2010
I had the GlobalCS.h and GlobalCS.cpp in the source directory. I prefer it this way because I find it makes finding files and code faster and in a large c++ project, just moving around the code base is a significant time waster. So much time could be saved writing c++ code if the IDE was designed to help find code faster. 2012 is A LOT better than 2010 so I'll give MSFT that but there could be a lot more features to that end (afterall VS has been around for almost 2 decades now)these types of persistent problems just get in the way of development. When I moved GlobalCS.h to the Header folder and cleaned the project and rebuilt, everything compiled as expected. The other similar error VS will throw at you is when the .h file is in the code directory (so #includes work) but not in the project. I got the same error messages when that happened and it took a good couple days to figure that one out. In a small project, it might not be as problematic but in big solution with multiple projects and dozens of files, it can be problematic.

Having trouble embedding Lua for Windows install into C++ program

This is the first question I have found myself not being able to get to the bottom of using my normal googling/stack overflowing/youtubing routine.
I am trying to compile a minimal Lua program inside of a C++ environment just to ensure my environment is ready to development. The Lua language will be later used for User Interface programming for my C++ game.
First some basic information on my environment:
Windows 7 64-bit
Visual studio 2010
Lua for Windows 5.1 (latest build I could download from google code)
Here is the code I am trying to compile:
// UserInt.cpp : Defines the entry point for the console application.
//
#pragma comment(lib,"lua5.1.dll")
#include "stdafx.h"
#ifndef __LUA_INC_H__
#define __LUA_INC_H__
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
int _tmain(int argc, _TCHAR* argv[])
{
lua_State * ls = luaL_newstate();
return 0;
}
#endif // __LUA_INC_H__
Here is the Error I am getting:
1>UserInt.obj : error LNK2019: unresolved external symbol _luaL_newstate referenced in function _wmain
1>c:\users\deank\documents\visual studio 2010\Projects\UserInt\Debug\UserInt.exe : fatal error LNK1120: 1 unresolved externals
Things I have tried:
I have read about lua_open()(and several other functions) no longer being used so I tried the newstate function instead. I get the same error. This was more of a sanity check than anything. I am using 5.1 and not 5.2 so I do not think this really matters.
I have also read this thread Cannot link a minimal Lua program but it does not seem to help me because I am not running the same environment as that OP. I am on a simple windows 7 and visual studio environment.
The top pragma comment line was something I saw in yet another thread. I get the same error with or without it.
I have gone into my visual studio C++ directories area and added the lua include to the includes and the lua lib to the libraries.
So it seems like my program is seeing the .h and seeing the symbol. But for some reason it is not getting the .cpp implementation for the functions. This is why I was hoping including that .dll directly would help fix the problem, but it hasn't.
So, I feel like I have exhausted all of my options solving this on my own. I hope someone is able to help me move forward here. Lua looks like an awesome language to script in and I would like to get my environment squared away for development.
I hope it is just some silly error on my part. I believe I have provided as much information as I can. If you need more specifics I will update with info if I can provide it.
Edit1
Tried the solution in this Can't build lua a project with lua in VS2010, library issue suspected
That did not work either.
You'll need to have the library (.LIB) file and add that to VS. Use Project > Properties and go to Linker > Input and add the full .lib filename to the "Additional Dependencies" line. Note that the .LIB is different from the .DLL.
Personally, I prefer adding the source code to my project, over referencing the dynamic link library. The following procedure will let you do as such.
Download the source code ( http://www.lua.org/ftp/ ), uncompress it.
In Visual Studio, choose File > New > Project and choose Visual C++, Win32, "Win32 Console Application".
In your project in Visual Studio, add all the source code, except luac.c. Also delete the main() function out of the file that VS created for you. This is usually given the name of the project you specified with the .cpp file extension. You could just remove this file all-together from the project.
Build and Run.
This is the Lua console

How to fix a linker error with PKEY_Device_FriendlyName

Upon using PKEY_Device_FriendlyName, I'm getting the following errors:
Error 1 error LNK2001: unresolved external symbol _PKEY_Device_FriendlyName DefaultAudioDeviceCPP.obj
Error 2 fatal error LNK1120: 1 unresolved externals C:\Users\srobertson\Documents\Visual Studio 2005\Projects\DefaultAudioDeviceCPP\Debug\DefaultAudioDeviceCPP.exe
What's a very simple way to clear these errors? I'm including functiondiscovery.h and functiondiscoverykeys.h. Also the path in Project->Properties...->Configuration Properties->C/C++->General->Additional Include Directories is correct.
EDIT: One thing of interest is that the errors are mentioning: _PKEY_Device_FriendlyName, not PKEY_Device_FriendlyName. But I'm only using the latter in my program.
Old post, but hopefully this answer will save someone some time.
I was having the same problem with DEVPKEY properties - like DEVPKEY_Device_FriendlyName. I got a very similar link error. I stumbled upon the answer in comments here:
Referencing GUIDs
Basically, add an #include before the include for things like devpkey.h where the property keys are defined.
So, at the top of my file I have:
#include <setupapi.h>
#include <initguid.h> // Put this in to get rid of linker errors.
#include <devpkey.h> // Property keys defined here are now defined inline.
An updated solution that worked for me, as per the microsoft docs
#include <functiondiscoverykeys.h>
PKEY_Device_FriendlyName resides in uuid.lib library. So you need to add a line to your source code:
#pragma comment(lib, "uuid.lib")
Most often, you can check with MSDN which library you need to reference.