Use the same makefile for make (Linux) and nmake (Windows)

前端 未结 8 1263
星月不相逢
星月不相逢 2020-12-13 19:26

I have a simple C program (one source file) which I want to compile on Linux and on Windows via make and nmake, respectively. Is there a possibility to accomplish this with

相关标签:
8条回答
  • 2020-12-13 20:29

    You should look at using CMake for this. With one source file, it should be quite easy. Here is how you could set up a simple project:

    cmake_minimum_required(VERSION 3.10)
    
    # set the project name
    project(Hello)
    
    # add the executable
    add_executable(Hello hello.c)
    

    To build the simple project, you would do the following (this assumes your source and CMakeLists.txt files are in the same directory as the source file hello.c:

    mkdir build
    cd build
    cmake ..
    cmake --build .
    
    0 讨论(0)
  • 2020-12-13 20:29

    I just thought of something completely different.

    If you stick to your extremely simple Makefile, which, you say, works, and just put the 'standard' variables CC and CFLAGS in your respective environments, say

      export CC=gcc
    

    respectively

      set CC=CL.EXE
    

    and

      export CFLAGS=-o myexecutable
    

    respectively

      set CFLAGS=/out:myexecutable.exe
    

    it might just work.

    Be aware, I'm not firm in the exact options to use, you'll have to figure them out yourself. But AFAIK both make variants recognize the same set of flags. You may even set those on the respective command lines (but not in the makefile, since NMAKE uses a different 'ifeq' syntax...)

    0 讨论(0)
提交回复
热议问题