Visual Express 2012 C++ error LNK2001 - c++

I am aware that this is a common error. I have been trying to resolve this for about a week now. I am using Visual Express 2012 and coding in C++. I start a new, but empy, win32 application. I then add a UI form to the project. In the solution there is a header file and a cpp file that I am working with. When I try and compile I get a error LNK2001 and LNK1120.
Error 1 error LNK2001: unresolved external symbol
__tWinMain C:\Users\Me\Desktop\visual c++ workspace\winFormExp01\LINK winFormExp01
Error 2 error LNK1120: 1 unresolved
externals C:\Users\Me\Desktop\visual c++
workspace\winFormExp01\Debug\winFormExp01.exe winFormExp01
The Header file:
#pragma once
namespace winFormExp01 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
The cpp file:
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
int Main(array<String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
winFormExp01::MyForm form;
Application::Run(%form);
return 0;
}
I am trying to get a basic windows application running with as little fat as possible. All help is very much appreciated. Thanks!

you have to use WinMain instead of main.
im not sure what WinMain should look like, but if you create a new 'Windows application', or 'Windows Forms' project from the templates, hopefully one of them will have it in it and you can copy the WinMain function deceleration.
you could try this?
void __stdcall WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
long lpCmdLine,
int nCmdShow)

The starting address is a function name from the C run-time library. The linker selects it according to the attributes of the program.
mainCRTStartup (or wmainCRTStartup) An application using /SUBSYSTEM:CONSOLE;
calls main (or wmain)
WinMainCRTStartup (or wWinMainCRTStartup) An application using /SUBSYSTEM:WINDOWS;
calls WinMain (or wWinMain), which must be defined with __stdcall
So, the linker is looking for WinMain in your program which it doesn't find and complains about.

Related

error LNK2001: unresolved external symbol Unreal Engine 4 Using Plugins (UE4)

I am trying to import a third party library for UE4 and use it in an actor. The UE4 documentation recommends creating a plugin to accomplish this and provides an auto generated template which i am trying to test currently.
I have created a blank C++ UE4 Project with a new third party custom plugin and an actor class in which to use the plugin/library.
This is my build.cs file for the main project:
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
public class TestGame : ModuleRules
{
public TestGame(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "CustomTestPlugin" });
PrivateDependencyModuleNames.AddRange(new string[] { "CustomTestPlugin" });
PublicIncludePaths.AddRange(new string[] { "../Plugins/CustomTestPlugin/Source/CustomTestPlugin/Public" });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
and have included the plugin in the actor cpp file using
#include "CustomTestPlugin.h"
as well as instantiating it in the same file:
FCustomTestPluginModule pluggin = FCustomTestPluginModule::FCustomTestPluginModule();
when I compile without the above it builds fine but when i declare the pluggin variable it gives the following error:
TestActor.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl FCustomTestPluginModule::StartupModule(void)" (?StartupModule#FCustomTestPluginModule##UEAAXXZ)
CustomTestPlugin.h is as follows:
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "Modules/ModuleManager.h"
class FCustomTestPluginModule : public IModuleInterface
{
public:
/** IModuleInterface implementation */
//FCustomTestPluginModule();
virtual void StartupModule() override;
virtual void ShutdownModule() override;
private:
/** Handle to the test dll we will load */
void* ExampleLibraryHandle;
};
and the cpp file:
// Copyright Epic Games, Inc. All Rights Reserved.
#include "CustomTestPlugin.h"
#include "Core.h"
#include "Modules/ModuleManager.h"
#include "Interfaces/IPluginManager.h"
#include "CustomTestPluginLibrary/ExampleLibrary.h"
#define LOCTEXT_NAMESPACE "FCustomTestPluginModule"
//FCustomTestPluginModule::FCustomTestPluginModule() {
// UE_LOG(LogTemp, Warning, TEXT("CustomPluConstructed"));
//}
void FCustomTestPluginModule::StartupModule()
{
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
// Get the base directory of this plugin
FString BaseDir = IPluginManager::Get().FindPlugin("CustomTestPlugin")->GetBaseDir();
// Add on the relative location of the third party dll and load it
FString LibraryPath;
#if PLATFORM_WINDOWS
LibraryPath = FPaths::Combine(*BaseDir, TEXT("Binaries/ThirdParty/CustomTestPluginLibrary/Win64/ExampleLibrary.dll"));
#elif PLATFORM_MAC
LibraryPath = FPaths::Combine(*BaseDir, TEXT("Source/ThirdParty/CustomTestPluginLibrary/Mac/Release/libExampleLibrary.dylib"));
#elif PLATFORM_LINUX
LibraryPath = FPaths::Combine(*BaseDir, TEXT("Binaries/ThirdParty/CustomTestPluginLibrary/Linux/x86_64-unknown-linux-gnu/libExampleLibrary.so"));
#endif // PLATFORM_WINDOWS
ExampleLibraryHandle = !LibraryPath.IsEmpty() ? FPlatformProcess::GetDllHandle(*LibraryPath) : nullptr;
if (ExampleLibraryHandle)
{
// Call the test function in the third party library that opens a message box
ExampleLibraryFunction();
}
else
{
FMessageDialog::Open(EAppMsgType::Ok, LOCTEXT("ThirdPartyLibraryError", "Failed to load example third party library"));
}
}
void FCustomTestPluginModule::ShutdownModule()
{
// This function may be called during shutdown to clean up your module. For modules that support dynamic reloading,
// we call this function before unloading the module.
// Free the dll handle
FPlatformProcess::FreeDllHandle(ExampleLibraryHandle);
ExampleLibraryHandle = nullptr;
}
#undef LOCTEXT_NAMESPACE
IMPLEMENT_MODULE(FCustomTestPluginModule, CustomTestPlugin)
any help in resolving this would be greatly appreciated thanks!
You should not need to instantiate your module manually. The module is loaded and instantiated by the engine at startup if it is referenced in your project.
If for some reason you need to get a reference to your module's singleton, you can use
FModuleManager::LoadModuleChecked<FYourModuleType>("YourModule");

Mutex is not supported when compiling with /clr or clr:pure (cpprestsdk aka casablanca)

I create a CLR project in visual c++ with 64 bit configuration, and try to use cpprestsdk aka casablanca 64bit.
But when I run the project, an error occured:
1>------ Build started: Project: Timestamp, Configuration: Debug x64 ------
1>MyForm.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.13.26128\include\mutex(8): fatal error C1189: #error: <mutex> is not supported when compiling with /clr or /clr:pure.
1>Testapi.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.13.26128\include\mutex(8): fatal error C1189: #error: <mutex> is not supported when compiling with /clr or /clr:pure.
1>Generating Code...
1>Done building project "Timestamp.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Other error:
E2093 a local lambda is not allowed in a member function of a managed class Timestamp c:\Users\Laptop-attendance\source\repos\Timestamp\Timestamp\Testapi.h 97
The IDE shows an error about '[' characters in the .then function like .then([=](http_response response), which if you pointed it out, it says "a local lambda is not allowed in a member function of a managed class"
If I try the cpprestsdk in a Windows Console Application of Visual c++ with 64 bit configuration, it works fine.
I'm using visual studio 2017.
Do you think cpprestsdk cannot be used in CLR project of vc++?
Thanks.
Here's my code, and the code about cpprestsdk I just got it from its tutorial:
#ifndef TESTAPI_H
#define TESTAPI_H
#pragma once
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/json.h>
namespace Timestamp {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
/// <summary>
/// Summary for Testapi
/// </summary>
public ref class Testapi : public System::Windows::Forms::Form
{
public:
Testapi(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Testapi()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// Testapi
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Name = L"Testapi";
this->Text = L"Testapi";
this->Load += gcnew System::EventHandler(this, &Testapi::Testapi_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Testapi_Load(System::Object^ sender, System::EventArgs^ e) {
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client client(U("http://13.231.231.252:3000/api/individual_employment_setting/detail/172"));
// Build request URI and start the request.
//uri_builder builder(U("/search"));
//builder.append_query(U("q"), U("cpprestsdk github"));
return client.request(methods::GET);
})
// Handle response headers arriving.
.then([=](http_response response)
{
printf("Received response status code:%u\n", response.status_code());
// Write response body into the file.
// return response.body().read_to_end(fileStream->streambuf());
stringstreambuf buffer;
response.body().read_to_end(buffer).get();
//show content in console
printf("Response body: \n %s", buffer.collection().c_str());
//parse content into a JSON object:
//json::value jsonvalue = json::value::parse(buffer.collection());
return fileStream->print(buffer.collection()); //write to file anyway
})
// Close the file stream.
.then([=](size_t)
{
return fileStream->close();
});
// Wait for all the outstanding I/O to complete and handle any exceptions
try
{
requestTask.wait();
}
catch (const std::exception &e)
{
printf("Error exception:%s\n", e.what());
}
}
};
}
#endif /*TESTAPI_H*/
At the very end of this answer, Hans Passant gives a hint which is useful in your case. Basically, you need a separate c++ library (clr support turned off) where you wrap the cpprest code, link this library from your CLR project and be sure no included headers will bring <mutex> in.
Just an example, have a class like this, in a restwrapper.h header file:
class RestWrapper
{
public:
void test();
};
In its implementation file, restwrapper.cpp:
#include "restwrapper.h"
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/json.h>
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
void RestWrapper::test()
{
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client client(U("http://13.231.231.252:3000/api/individual_employment_setting/detail/172"));
//etc ...
}
You can compile this class in a DLL (linking cpprest and all its related libraries) then make your CLR project link that library. In the CLR project you need to include restwrapper.h only, which in turn includes nothing:
#include <restwrapper.h>
System::Void Testapi_Load(System::Object^ sender, System::EventArgs^ e)
{
RestWrapper wrapper;
wrapper.test();
}
You can call a class that uses mutexes from a CLR project, what you need to do is create a normal c++ project and create classes that have the required functionality but don't expose <mutex> in their headers, you are then free to call these classes from your CLR project.
Just right click your .cpp file, and under "General -> Common Language Runtime Support" select "No Common Language RunTime Support".
Simplest solution really.

A member-function of a managed type cannot be compiled as an unmanaged function

This is the 2nd form in my program, and it generates the above error. The constructor function is what generates the error, and I can't see why. It's pretty much the same as my main window's constructor, which works just fine.
The only difference is that this one takes an argument. (even if I remove the argument in the SettingsForm constructor, and revert back to void, I still get the same error.
Can anybody tell me why it seems to think this constructor is being compiled as an unmanaged
function?
SettingsForm.h
#pragma once
#pragma managed(push, off)
#include "SpriteyData.h"
#pragma managed(pop)
namespace Spritey {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace SpriteyData;
/// <summary>
/// Summary for SettingsForm
/// </summary>
public ref class SettingsForm : public System::Windows::Forms::Form
{
public:
SpriteySettings* currentSetLocCopy;//A local copy of our current settings.
SettingsForm(SpriteySettings* currentSettings)<------ERROR OCCURS HERE
{
InitializeComponent();
currentSetLocCopy = new SpriteySettings(*currentSettings); //take a copy of our current settings
//initialise the elements on our form to the values stored in the SpriteySettings
this->anchorDevCheckBox->Checked = currentSetLocCopy->isAnchorDevAllowed();
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~SettingsForm()
{
if (components)
{
delete components;
}
if(currentSetLocCopy)
{
delete currentSetLocCopy;
}
}
private: System::Windows::Forms::Button^ CancelButton;
private: System::Windows::Forms::Button^ ApplyButton;
private: System::Windows::Forms::GroupBox^ editorSettingsGroup;
private: System::Windows::Forms::CheckBox^ anchorDevCheckBox;
private:
Note: The above is just the constructor + a bit more of the code, and is merely a code sample of the part that is causing the error.
Also this is a mixed managed & unmanaged project.
Repro for this compile error:
#pragma managed(push, off)
class SpriteySettings {};
ref class Test
{
public:
Test(SpriteySettings* arg) {}
};
error C3280: 'Test::Test' : a member-function of a managed type cannot be compiled as an unmanaged function
As well as a slew of additional errors. So the diagnostic is that this code is getting compiled without the /clr compile option in effect. Since this is a .h file, the likely cause is you #including it in a .cpp file that is being compiled without /clr. You'll need to find that #include directive.
I was also facing the same issue. On adding .Net Target Framework Version as v4.5.1 (which was missing previously) , the issue was resolved

C++ Error: name followed by '::' must be a class or namespace name. WindowsForm in DLL

So i made a windows32 C++ dll application in visual studio 2012 and then i added a windows form in the header files section and gave it the name "UserInterface.h". When i clicked the Add button i got a popup saying "You are adding a CLR component to a native project. Your project will be converted to have Common Language Runtime support. Do you wish to continue?" and i clicked yes and it made the files "UserInterface1.cpp" and "UserInterface1.h".
but in the "UserInterface1.h" there are errors all over. here are its contents:
#pragma once
namespace AssultCubeDLL {
//ERRORS HERE: ******************************************************
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for UserInterface
/// </summary>
// ERROS HERE: *********************************************************
public ref class UserInterface : public System::Windows::Forms::Form
{
public:
UserInterface(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~UserInterface()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
// ERRORS HERE: ************************************************
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// UserInterface
// ERRORS HERE: *******************************************************
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Name = L"UserInterface";
this->Text = L"UserInterface";
this->Load += gcnew System::EventHandler(this, &UserInterface::UserInterface_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void UserInterface_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
I added comments to where the errors pop up like "Error: name followed by '::' must be a class or namespace name." does anyone know why i am getting these problems?
You will need to create a mixed mode application. Microsoft has clear instructions for the required steps.
The MS instructions will resolve the problem for System and System::Collections, but does not resolve the issues for System::ComponentModel, System::Windows::Forms, System::Data, and System::Drawing.
To compile, you must add references for the missing DLLs to the application. You can remove the using <System.Windows.Forms> from the stdafx.h file. Right-click properties and select References..., then select Add New Reference, then check the following DLLs
System
System.Data
System.Drawing
System.Windows.Forms
The code will now compile.

Windows Forms - Single Instance - Include Statement

When I try to use #include "CFIS_Main.h" statement in form "For_Student_Details.h",
Its not accepting...Anybody can point me the mistake? Thanks for the helps..
MyProject.cpp
// MyProject.cpp : main project file.
#include "stdafx.h"
#ifndef CFIS_Main_h
#define CFIS_Main_h
#include "CFIS_Main.h"
#endif
using namespace MyProject;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew CFIS_Main());
return 0;
}
My Codes from MdiParent
//CFIS_Main.h IsMdiContainer = True
#include "For_Student_Detials"
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
For_Student_Detials^ MyStudentDet= For_Student_Detials::GetForm(true,this);
MyStudentDet->MdiParent=this;
MyStudentDet->FormBorderStyle=System::Windows::Forms::FormBorderStyle::None;
MyStudentDet->Dock=DockStyle::Fill;
MyStudentDet->Show();
}
My Codes From MdiChild For_Student_Details
#include "CFIS_Main.h" Why Not included...?????
public: static For_Student_Details^ For_Student_Details::_instance = nullptr;
public: static For_Student_Details^ For_Student_Details::GetForm(bool^ IsMDIChild, CFIS_Main^ MyInstFrm) {
if (_instance == nullptr)
_instance = gcnew For_Student_Details();
if (_instance->IsDisposed)
_instance = gcnew For_Student_Details();
if (IsMDIChild)
_instance->MdiParent = MyInstFrm;
return _instance;
}
Receiving The Below errors
error C2061: syntax error : identifier 'CFIS_Main'
error C2065: 'MyInstFrm' : undeclared identifier
error C2660: 'CashFlow_InformationsSystem::For_Loan_Details::GetForm' : function does not take 2 arguments
From the above code, Its not including CFIS_Main, I can't identify my mistake, Does anybody can point me?
Thanks For The Helps
You have a circular header reference:
"For_Student_Details" includes "CFIS_Main.h"
"CFIS_Main.h" includes "For_Student_Details"
You will need to resolve this circular dependency.
The easiest way to do so is to leave only the function declaration for button1_Click() in "CFIS_Main.h" and move the definition into "MyProject.cpp", where you also include "For_Student_Details".
You will also have to define (or include the right header) the type CFIS_Main referenced in For_Student_Details::GetForm() (this might be resolved once you fix the circular include problem)
Also, place the include guards in your header files, not the .cpp files