7

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
loneraver
  • 1,282
  • 2
  • 15
  • 22
  • TARGET_INCLUDE_DIRECTORIES(mainProgram PUBLIC exports}) - I think there is an unwanted curly brace at the end. – sebenalern May 10 '16 at 03:12
  • 1
    Thanks and nice catch, but that was a copy and paste error. I corrected it in my post. Still the same problem. Great eyes for detail though. – loneraver May 10 '16 at 03:42
  • The file might be accidentally locked. Try from a clean build directory. – Antonio May 10 '16 at 12:38
  • I'm been mainly using GCC/MinGW so working with the MSVC is new for me so thank you for baring with me with any stupid questions I might have. Do I need to hard code the "#include MyLibrary_Export.h" with all my libraries and do I need to hard code "MyLibrary_EXPORT" to all my functions in my header to get a shared library to compile with exported symbols in MSVC? – loneraver May 10 '16 at 15:07
  • @loneraver If you are using CMake 3.4 or above and you just want to export all your symbols you can just define `CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS`. For the complete solution, please see my answer below. – Florian May 10 '16 at 18:49

1 Answers1

9

Turning my comment into an answer

For CMake Version >= 3.4

Just use CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS:

cmake_minimum_required(VERSION 3.4)
project(helloWorld)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS 1)

add_library(MyLibrary SHARED myShared.cpp)

set(SOURCE_FILES main.cpp)
add_executable(mainProgram ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(mainProgram MyLibrary)

For CMake Version < 3.4

You need to declare your functions/symbols as exported. So in your case, you have to modify the following files:

myShared.h

#ifndef HELLOWORLD_HELLO_H
#define HELLOWORLD_HELLO_H

#include "MyLibrary_Export.h"

void MyLibrary_EXPORT sayHI();

#endif //HELLOWORLD_HELLO_H

CMakeLists.txt

cmake_minimum_required(VERSION 3.4)
project(helloWorld)

set(CMAKE_CXX_STANDARD 11)
include(GenerateExportHeader)

add_library(
    MyLibrary 
    SHARED 
        myShared.cpp
        myShared.h
        MyLibrary_Export.h   
)
GENERATE_EXPORT_HEADER( 
    MyLibrary
    BASE_NAME MyLibrary
    EXPORT_MACRO_NAME MyLibrary_EXPORT
    EXPORT_FILE_NAME MyLibrary_Export.h
    STATIC_DEFINE MyLibrary_BUILT_AS_STATIC
)
target_include_directories(MyLibrary PUBLIC "${CMAKE_CURRENT_BINARY_DIR}")

set(SOURCE_FILES main.cpp)
add_executable(mainProgram ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(mainProgram MyLibrary)

I've

  1. added MyLibrary_EXPORT to your function declaration
  2. added the CMAKE_CURRENT_BINARY_DIR to the shared libraries include paths (to point to the generated export definitions header)
  3. made use of the more cross-platform CMAKE_CXX_STANDARD definition.

Alternative

References

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
  • It works!!! That solved everything. I can't thank you enough. This makes me wish that learning these awesome tricks were so much easier. – loneraver May 10 '16 at 23:15