Fatal error LNK1104 with Visual Studio 2017 and CMake - cmake

I have the following header in a file matrix_utils.hpp :
#include "someIncludes"
void ReadMtxMatrixHeader(){}
int ScanCurrentLine() {}
void ReadMtxMatrixHeader( {}
template<typename T> void RTS_EXPORTS ReadMtxMatrixToCSR() {}
template<typename T> void RTS_EXPORTS ReadMtxMatrixToColMajorArray() {}
The implementation here is not important so I left empty brackets.
The RTS_EXPORTS is a macro for :
# define RTS_EXPORTS __declspec(dllexport) // if WIN32
# define RTS_EXPORTS __attribute__ ((visibility ("default"))) // if GNUC >=4
This header is part of a module called "utils" and is used by a target "rts_test_utils". This code works perfectly fine on Linux and on Windows using Cygwin and CLion. But as soon as I try to use Visual Studio 2017 (my project is a CMake project), I get this error when building "rts_test_utils" :
$buildPath\build\x86-Debug\modules\utils\LINK : fatal error LNK1104:
cannot open file '..\..\lib\Debug\rts_utilsd.lib'
Visual Studio generates the DLL for rts_utils, but not the lib. If I add a dummy class like the one below to "matrix_utils.hpp", then it works and it creates a rts_utilsd.lib. Why ?
class RTS_EXPORTS Foo{};

Following comment from oLen, I added the following code in my matrix_utils.cpp (could have been in the header too) to force compiler to see that a template function is exported:
template void RTS_EXPORTS
ReadMtxMatrixToColMajorArray(std::vector<float> &col_major_host_ptr,
const std::string filename, bool is_one_based);
I know I will use this function with float specialization, so it's not even wasted.

Related

Xcode 11 Objective C++ - Can't separate implementation from Header in ObjectiveC++ Project

I have created an Xcode standard Objective-C Project wanting to test integration with C++.
I have renamed my file accordingly to .mm etc.
I have created a C++ Class with a .hpp header and a .cpp imp file.
This is the header
class a {
private:
int anInt = 1;
public:
int getAnInt();
};
this is the imp
#include "PrismMatricies.hpp"
int a::getAnInt() {return anInt;}
in my ViewController.mm file I do this after importing the .hpp header
a ar;
NSLog(#"Some Log %i", ar.getAnInt());
Build Error Code in Xcode =
Undefined symbol: a::getAnInt()
If I move the function definition inside the header like this
class a {
private:
int anInt = 1;
public:
int getAnInt();
};
int a::getAnInt() {return anInt;}
The project works
Can anyone explain this to me as I simply don't get it
Note that "undefined symbol" is a link error, not a compile error, and your code certainly looks correct.
You will get that error if your .cpp isn't being linked into your build target. Make sure it's included in your build target's "compile sources" list. (You can also check that the target membership box is ticked in the File Inspector utility pane.)

why i can't wrap methods from one dll but can do it for another?

Im trying to write a wrapper in c++/cli for an DLL, which code i dont have, only DLL file and header but i created lib file through VS command prompt. When i`m trying to build solution i receive this errors:
DotNetWrappOfAsterSdkDll.obj : error LNK2028: unresolved token (0A00002E) "void __stdcall MuteClearLastError(void)" (?MuteClearLastError##$$FYGXXZ) referenced in function "public: void __clrcall DotNetWrappOfAsterSdkDll::WrapperClass2::doMuteClearLastError(void)" (?doMuteClearLastError#WrapperClass2#DotNetWrappOfAsterSdkDll##$$FQ$AAMXXZ)
DotNetWrappOfAsterSdkDll.obj : error LNK2019: unresolved external symbol "void __stdcall MuteClearLastError(void)" (?MuteClearLastError##$$FYGXXZ) referenced in function "public: void __clrcall DotNetWrappOfAsterSdkDll::WrapperClass2::doMuteClearLastError(void)" (?doMuteClearLastError#WrapperClass2#DotNetWrappOfAsterSdkDll##$$FQ$AAMXXZ)
I tried to create my own DLL and include it to the wrapper, and its working perfectly
here dll created by me which i can use in c++/cli wrapper:
//header file
#pragma once
#define DLLEXP __declspec( dllexport )
namespace Computations {
DLLEXP void someMethod(int number);
}
//cpp file
#include "Computations.h"
#include <iostream>
#include <time.h>
//#include "pnl/pnl_random.h"
using namespace std;
void Computations::someMethod(int number)
{
std::cout << "something "<<number*number << endl;
}
and here is part of header of DLL which i want to use:
#ifndef MUTEIFC_H
#define MUTEIFC_H
#include <Windows.h>
#ifdef MUTEIFC_LIBRARY
# define MUTEAPI extern "C"__declspec(dllexport)
#else
# define MUTEAPI __declspec(dllimport)
#endif
#define MUTECALL __stdcall
/** \ingroup init */
/** Initialization of the ASTER SDK library
* \returns TRUE - success, FALSE - failure (use \ref MuteLastErrorCode or/and \ref MuteLastErrorInfo to get
* failure cause)
* \note This function will require Administrative privileges on the first call on a given computer.
*/
MUTEAPI BOOL MUTECALL MuteIfcInitialize(VOID);
/** \ingroup init */
/** Finialization of the ASTER SDK library
*/
MUTEAPI VOID MUTECALL MuteIfcFinalize(VOID);
/** \ingroup errors*/
/** Clears the calling thread's last-error code and description.
* The last-error is maintained on a per-thread basis. Multiple threads do not overwrite each other's last-error.
*/
MUTEAPI VOID MUTECALL MuteClearLastError(VOID);
#endif // MUTEIFC_H
and my c++/cli code :
//header file
#pragma once
#include "Computations.h"
#include "muteifc.h"
using namespace System;
namespace DotNetWrappOfAsterSdkDll
{
public ref class WrapperClass2
{
public:
void doMuteClearLastError();
};
public ref class WrapperClass
{
private:
public:
void getPriceCallEuro(int number);
};
}
//cpp file
#include "DotNetWrappOfAsterSdkDll.h"
using namespace DotNetWrappOfAsterSdkDll;
using namespace Computations;
namespace DotNetWrappOfAsterSdkDll
{
//this dont work
void WrapperClass2::doMuteClearLastError() {
MuteClearLastError();
}
//this works great
void WrapperClass::getPriceCallEuro(int number) {
someMethod(number);
//MuteIfcFinalize();
}
}
Please tell me what i'm doing wrong
You probably didn't add the lib that contains the function reference to the linker options.
Either the lib contains he code it self or it has a reference to the DLL that must be loaded. The linker will bring your code and the DLL (or static lib) code together...

Swift doesn't allow function overloading in objective-c or c headers

I have some swift code, which is not really important in context of question.
Also i have Objective-C bridging header ObjC-Swift-Bridging.h:
#import "my_objective_cpp_header.h"
And i have free C/C++ - style functions in that header:
inline void foo() {
}
inline void foo(int i) {
}
When i try to compile it in xcode 9.2 with enabled swift 4.0 i get error:
my_objective_cpp_header.h:29:13: error: redefinition of 'foo'
inline void foo(int i){
^
my_objective_cpp_header.h:26:13: note: previous definition is here
inline void foo() {
^
ObjC-Swift-Bridging.h:22:9: note: in file included from ObjC-Swift-Bridging.h:22:
#import "my_objective_cpp_header.h"
The same happens if i put these functions to C/C++ header and write
#include "my_cpp_header.h"
into ObjC-Swift-Bridging.h
So... Swift doesn't support code which was supported in Objective-C++? Did i miss something?

Build Windows DllMain DLL with CMake

I have a question similar to this one. I am trying to compile a DLL for windows similar to how Visual Studio would, except with CLion and CMake. I've tried the answer in the question, as well as the steps shown here, but I still get an error while injecting.
My dll code is very simple, a similar dll compiled in visual studio works fine:
#include <windows.h>
#include <iostream>
using namespace std;
void hello() {
AllocConsole();
freopen("CONOUT$", "w", stdout);
cout << "Hello, World!" << endl;
}
bool __stdcall DllMain(HMODULE /*module*/, DWORD reason, LPVOID /*reserved*/) {
if (reason == DLL_PROCESS_ATTACH) hello();
return true;
}
Also, here's what I tried in CMakeLists.txt: sorry, there should have been a space between PROJECT_NAME and MODULE
cmake_minimum_required(VERSION 3.9)
project(PROJECT_NAME)
include (GenerateExportHeader)
set(CMAKE_CXX_STANDARD 17)
add_library(PROJECT_NAME MODULE main.cpp)
set_target_properties(PROJECT_NAME PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
GENERATE_EXPORT_HEADER(PROJECT_NAME
BASE_NAME PROJECT_NAME
EXPORT_MACRO_NAME PROJECT_NAME_EXPORT
EXPORT_FILE_NAME PROJECT_NAME_Export.h
STATIC_DEFINE PROJECT_NAME_BUILT_AS_STATIC)
You have two options:
Add the BUILD_SHARED_LIBS variable to CMake's cache as a boolean value then check it. This will modify the behaviour of the add_library command to make a shared library i.e. a DLL file on Windows.
Explicitly create the library as shared: add_library(PROJECT_NAMEMODULE SHARED main.cpp)
BUILD_SHARED_LIBS variable documentation: https://cmake.org/cmake/help/v3.10/variable/BUILD_SHARED_LIBS.html

Singleton MFC extension DLL

I declare a singleton on a MFC extension DLL, like this:
//header file: SingleTon.h
class AFX_EXT_CLASS CMySingleton
{
public:
static CMySingleton* Instance()
{
if(!singleton)
singleton = new CMySingleton();
return singleton;
}
int a;
// Other non-static member functions
private:
CMySingleton() {}; // Private constructor
CMySingleton(const CMySingleton&); // Prevent copy-construction
CMySingleton& operator=(const CMySingleton&); // Prevent assignment
virtual ~CMySingleton() {};
static CMySingleton* singleton;
};
And in a cpp file I code the following line:
CMySingleton* CMySingleton::singleton = NULL;
Code 2:
CMySingleton *a;
a = CMySingleton::Instance();
The problem is when I code "code 2" in a Regular Dll, all works fine, but when I code "code 2" in another MFC extension DLL gives an error:
unresolved external symbol "private: static class CMySingleton* CMySingleton::singleton" (?singleton#CMySingleton##0PAV1#A)
I check correctly all the dependencies, via Project Dependencies.
Any idea?
The problem is in the AFX_EXT_CLASS macro.
#ifdef _AFXEXT
#define AFX_EXT_CLASS __declspec(dllexport)
#else
#define AFX_EXT_CLASS __declspec(dllimport)
#endif
Extension dll defines _AFXEXT and your class is exported, and main app (or a regular dll) doesn't define it so it's imported. But your second extension dll also defines _AFXEXT and your class declaration uses dllimport instead of dllexport and you get a linker error. The solution is to create your own macro for both dlls and use them instead of AFX_EXT_CLASS:
#ifdef EXTENSION_ONE
#define EXT_CLASS_ONE __declspec(dllexport)
#else
#define EXT_CLASS_ONE __declspec(dllimport)
#endif
Create EXTENSION_TWO and EXT_CLASS_TWO for your second dll. Define EXTENSION_ONE only in your first extension dll project, and EXTENSION_TWO only in your second extension dll project.