0

I am trying to generate a shared library with cmake.

When I have this CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(test VERSION 0.0.1 LANGUAGES C DESCRIPTION "test shared library example")

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED True)



##################################################
# Create target and set properties
##################################################

set(CMAKE_BUILD_TYPE Release)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

file(GLOB SOURCES "src/*.c")

# generate shared library
add_library(test SHARED
    ${SOURCES})

I go to command line (which is msys) and generate MSYS Makefiles with mkdir build && cd build && cmake . -G "MSYS Makefiles and run make. This works without issues. Works great.

Then I thought I need to automate it rather than providing -G MSYS Makefiles from the command line manually, so I modified CMakeLists.txt as follows:

cmake_minimum_required(VERSION 3.20)
project(test VERSION 0.0.1 LANGUAGES C DESCRIPTION "test shared library example")

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED True)



##################################################
# Create target and set properties
##################################################

set(CMAKE_BUILD_TYPE Release)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

file(GLOB SOURCES "src/*.c")

if (CMAKE_SYSTEM_NAME STREQUAL "MSYS")
    set(CMAKE_GENERATOR "MSYS Makefiles")
elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND (NOT CMAKE_SYSTEM_NAME MATCHES "MSYS"))
    if (CMAKE_C_COMPILER_ID STREQUAL "MSVC")
        set(CMAKE_GENERATOR "NMake Makefiles")
    else (CMAKE_C_COMPILER_ID STREQUAL "GNU")
        set(CMAKE_GENERATOR "MinGW Makefiles")
    endif()
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
    if (CMAKE_C_COMPILER_ID MATCHES "Clang|AppleClang|GNU")
        set(CMAKE_GENERATOR "Unix Makefiles")
    endif()
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
    if (CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
        set(CMAKE_GENERATOR "Unix Makefiles")
    endif()
endif()





# generate shared library
add_library(test SHARED
    ${SOURCES})

After reading docs on cmake and going through SO, I was expecting it to work. But when I run cmake .., I get following error:

-- Building for: NMake Makefiles
CMake Error at CMakeLists.txt:2 (project):
  Running

   'nmake' '-?'

  failed with:

   The system cannot find the file specified


CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
See also "C:/Users/suman/bishnu/test/build/CMakeFiles/CMakeOutput.log".

Yes, the error reveals CMAKE_C_COMPILER not set. I do not want to do it manually as the location of compilers may be variable. But definitely, the compilers are available from command line.

I saw similar question in SO which was not relevant as to the automation.

Actually I don't have MSVC compiler, I want to do it with msys and gcc.

Any idea, how I can get over with this issue? Is it doable or is it not possible at all?


This is my OS information:

Host Name:                 SUMANKHANAL
OS Name:                   Microsoft Windows 10 Pro
OS Version:                10.0.19043 N/A Build 19043
OS Manufacturer:           Microsoft Corporation
OS Configuration:          Standalone Workstation
OS Build Type:             Multiprocessor Free
Registered Owner:          N/A
Registered Organization:   N/A
Product ID:                00331-20350-00000-AA867
Original Install Date:     2/19/2022, 1:41:50 PM
System Boot Time:          4/8/2022, 11:31:17 AM
System Manufacturer:       Dell Inc.
System Model:              Inspiron 5379
System Type:               x64-based PC
Processor(s):              1 Processor(s) Installed.
                           [01]: Intel64 Family 6 Model 142 Stepping 10 GenuineIntel ~2001 Mhz
BIOS Version:              Dell Inc. 1.17.0, 8/18/2021
Windows Directory:         C:\WINDOWS
System Directory:          C:\WINDOWS\system32
Boot Device:               \Device\HarddiskVolume1
System Locale:             en-us;English (United States)
Input Locale:              en-us;English (United States)
Time Zone:                 (UTC+05:45) Kathmandu
Total Physical Memory:     8,025 MB
Available Physical Memory: 1,700 MB
Virtual Memory: Max Size:  15,470 MB
Virtual Memory: Available: 2,406 MB
Virtual Memory: In Use:    13,064 MB
Page File Location(s):     C:\pagefile.sys
Domain:                    WORKGROUP
Logon Server:              \\SUMANKHANAL
Hotfix(s):                 5 Hotfix(s) Installed.
                           [01]: KB5010472
                           [02]: KB5000736
                           [03]: KB5011487
                           [04]: KB5011352
                           [05]: KB5005699
Network Card(s):           2 NIC(s) Installed.
                           [01]: Qualcomm QCA61x4A 802.11ac Wireless Adapter
                                 Connection Name: Wi-Fi
                                 DHCP Enabled:    Yes
                                 DHCP Server:     192.168.1.254
                                 IP address(es)
                                 [01]: 192.168.1.83
                                 [02]: fe80::d948:4175:e48d:b886
                                 [03]: 2400:1a00:b111:8baa:b5ba:372b:ea23:d78b
                                 [04]: 2400:1a00:b111:8baa:d948:4175:e48d:b886
                                 [05]: 2400:1a00:b111:8baa::2
                           [02]: Bluetooth Device (Personal Area Network)
                                 Connection Name: Bluetooth Network Connection
                                 Status:          Media disconnected
Hyper-V Requirements:      VM Monitor Mode Extensions: Yes
                           Virtualization Enabled In Firmware: Yes
                           Second Level Address Translation: Yes
                           Data Execution Prevention Available: Yes
Suman Khanal
  • 2,977
  • 15
  • 26
  • 1
    You cannot set CMake generator inside `CMakeLists.txt`. If you want set **default** CMake generator in your system (so passing `-G` option to `cmake` won't be required), then you could set environment variable `CMAKE_GENERATOR`. – Tsyvarev Apr 10 '22 at 14:00

0 Answers0