This question already has an answer here:
- cmake link shared library on Windows 2 answers
I've been building C and C++ projects in unix environments using CMake. However, I want to also start compiling in MSVC and I'm trying to get my head around the cmake documentation but I'm getting stuck. I keep getting the following message when I try to build.
LINK : fatal error LNK1104: cannot open file 'Debug\MyLibrary.lib' [C:\sandbox\projects\cpp\DummyChelloWorld\build\ma inProgram.vcxproj]
Can you tell me what I'm doing wrong?
CMakeLists.txt
cmake_minimum_required(VERSION 3.4)
project(helloWorld)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include(GenerateExportHeader)
ADD_LIBRARY(MyLibrary SHARED myShared.cpp)
set(SOURCE_FILES main.cpp)
GENERATE_EXPORT_HEADER( MyLibrary
BASE_NAME MyLibrary
EXPORT_MACRO_NAME MyLibrary_EXPORT
EXPORT_FILE_NAME ${CMAKE_BINARY_DIR}/MyLibrary_Export.h
STATIC_DEFINE MyLibrary_BUILT_AS_STATIC
)
add_executable(mainProgram ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(mainProgram MyLibrary)
TARGET_INCLUDE_DIRECTORIES(mainProgram PUBLIC exports)
main.cpp
#include "myShared.h"
int main() {
sayHI();
return 0;
}
myShared.cpp
#include <iostream>
#include "myShared.h"
using namespace std;
void sayHI() {
cout << "Hello World lib" << endl;
}
myShared.h
#ifndef HELLOWORLD_HELLO_H
#define HELLOWORLD_HELLO_H
void sayHI();
#endif //HELLOWORLD_HELLO_H