# External project using makefile

**URL:** https://discourse.cmake.org/t/external-project-using-makefile/2692
**Category:** Usage
**Tags:** os:linux
**Created:** [February 4, 2021, 9:00am UTC](https://discourse.cmake.org/t/external-project-using-makefile/2692 "2021-02-04T09:00:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Desmond](https://discourse.cmake.org/user_avatar/discourse.cmake.org/desmond/32/1176_2.png) [@Desmond](https://discourse.cmake.org/u/Desmond)
#### Post date: [February 4, 2021, 9:00am UTC](https://discourse.cmake.org/t/external-project-using-makefile/2692/1 "2021-02-04T09:00:41Z")

</div>

Hi all,

I’m incorporating lua-jit into a larger cross-compilation project, I’d like to invoke the Makefile of lua-jit using some cross platform environment variables.

The lua-jit source code is already present in my repository.

As a first step I’ve added a CMakeLists.txt file to the root folder of lua-jit, so I could do add\_subdirectory() from the larger project.

I’m not very familiar with cmake so any help will be appreciated.

**Source**  
Using “SOURCE\_DIR ${CMAKE\_CURRENT\_LIST\_DIR}”

**Build**

Build command “make -C \<SOURCE\_DIR\>…”  
Mainly I’m looking to override 2 environment variables:  
**HOST\_CC** - lua-jit is building some compilation utils, how can I set this to the default compiler in the system?  
**CC** - should pick the toolchain compiler that is used by the larger project, how can I get the toolchain compiler path?

**Install**  
Where should I install the lua-jit library, I simply want to link with it, how can I do this?

Thanks!

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 4, 2021, 4:44pm UTC](https://discourse.cmake.org/t/external-project-using-makefile/2692/2 "2021-02-04T16:44:45Z")

</div>

I think there are two ways to go about this:

- adding a `CMakeLists.txt` to LuaJIT and using it directly (i.e., making your own `add_library` calls) (note that others may have already done this and searching GitHub for such examples may be of use); or
- using `FetchContent` (which does the `ExternalProject` for you) and should make LuaJIT available for use in the rest of your project.

@craig.scott is way more versed in these bits than I am, so he likely would have more useful information beyond just these pointers.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [February 4, 2021, 11:04pm UTC](https://discourse.cmake.org/t/external-project-using-makefile/2692/3 "2021-02-04T23:04:05Z")

</div>

Maybe something like this (untested, but should get you close):

```cmake
ExternalProject_Add(lua-jit
    SOURCE_DIR /path/to/wherever/you/put/it # This is not likely to be CMAKE_CURRENT_LIST_DIR
    INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/install # Just an example
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ${CMAKE_COMMAND} -E env
        HOST_CC=... # See below
        CC=${CMAKE_C_COMPILER}
        make -C <SOURCE_DIR>
    BUILD_IN_SOURCE TRUE
    INSTALL_COMMAND make DESTDIR=<INSTALL_DIR> install
)

```

There’s a number of assumptions baked into the above to keep the example simple:

- The command `make` is always available on the host system, it reachable on the PATH and is the appropriate make tool to use.
- `make DESTDIR=<somewhere> install` does the expected thing and installs the things you want under the location you specify with `DESTDIR`.
- The `CMAKE_C_COMPILER` variable holds only the executable to run the compiler and no additional command-line arguments (a feature that is only supported for CMake 3.19 or later anyway, from what I can tell).
- No configure step is required.
- The lua-jit project should be built in-source. This is usually something you want to avoid, but I don’t know if luajit supports an out-of-source build.

I can’t quite tell what you expect for the `HOST_CC` compiler. You want to specify to use the default without actually working out/knowing what that default is? You will need to work out what to specify for that yourself and put it at the place shown in the example.

With the above, you then have to work out where the library you want to use in the main project gets installed to by ExternalProject. The following is just a guess:

```cmake
add_executable(someApp ....)

target_link_libraries(someApp ${CMAKE_CURRENT_BINARY_DIR}/install/lib/liblui-jit.a)
add_dependencies(someApp lua-jit)

```

Again, the above is untested and makes assumptions about what kind of library is built and where it gets installed. You will need to investigate yourself and work out the kinks from here.

---

<div class="post-metadata">

### Author: ![Judy\_Wei](https://discourse.cmake.org/user_avatar/discourse.cmake.org/judy_wei/32/2796_2.png) [@Judy\_Wei](https://discourse.cmake.org/u/Judy_Wei)
#### Post date: [September 25, 2022, 10:31pm UTC](https://discourse.cmake.org/t/external-project-using-makefile/2692/4 "2022-09-25T22:31:51Z")

</div>

Hi, I’m trying to write a CMake to include the luajit but it still does not work. Can you tell me why? Thanks a lot!

My CMakeLists.txt

```auto
cmake_minimum_required(VERSION 3.10)

project(myapp LANGUAGES C CXX)
include(ExternalProject)
ExternalProject_Add(project_luajit
  #URL http://luajit.org/download/LuaJIT-2.0.1.tar.gz
  SOURCE_DIR "/users/weicuidi/cmake-v3/out/build/project_luajit-prefix/src/project_luajit"
  DOWNLOAD_COMMAND ""
  #PREFIX ${CMAKE_CURRENT_BINARY_DIR}/luajit-2.0.1
  CONFIGURE_COMMAND ""
  BUILD_COMMAND make
  BUILD_IN_SOURCE true
  INSTALL_COMMAND make install
)
ExternalProject_Get_Property(project_luajit SOURCE_DIR)
message("+++++" ${SOURCE_DIR})
#message("+++++install_dir:" ${install_dir})
add_library(luajit STATIC IMPORTED)
##./out/build/project_luajit-prefix/src/project_luajit/src/libluajit.a
set_property(TARGET luajit PROPERTY IMPORTED_LOCATION ${SOURCE_DIR}/src/libluajit.a)
add_dependencies(luajit project_luajit)
add_executable(${PROJECT_NAME} main.cpp)
include_directories(${SOURCE_DIR}/src)
target_link_libraries(${PROJECT_NAME} luajit)
target_link_libraries(${PROJECT_NAME} ${SOURCE_DIR}/src/libluajit.a)

```

I can cmake successfully but have an issue when I build. I have a very simple main.cpp which only calls the function print\_usage() in luajit.h.  
./build.sh  
[80%] Built target project\_luajit  
[90%] Building CXX object CMakeFiles/myapp.dir/main.cpp.o  
/users/wcd/cmake-v3/main.cpp: In function ‘int main()’:  
/users/wcd/cmake-v3/main.cpp:12:5: error: ‘print\_usage’ was not declared in this scope  
12 | print\_usage();  
| ^ ~~~~~~~~~~  
make[2]: \*\*\* [CMakeFiles/myapp.dir/build.make:63: CMakeFiles/myapp.dir/main.cpp.o] Error 1  
make[1]: \*\*\* [CMakeFiles/Makefile2:78: CMakeFiles/myapp.dir/all] Error 2  
make: \*\*\* [Makefile:84: all] Error 2

---

<div class="post-metadata">

### Author: ![Judy\_Wei](https://discourse.cmake.org/user_avatar/discourse.cmake.org/judy_wei/32/2796_2.png) [@Judy\_Wei](https://discourse.cmake.org/u/Judy_Wei)
#### Post date: [September 26, 2022, 2:50am UTC](https://discourse.cmake.org/t/external-project-using-makefile/2692/5 "2022-09-26T02:50:32Z")

</div>

Solved it by the following code.

```auto
ExternalProject_Add(project_luajit
  #URL http://luajit.org/download/LuaJIT-2.0.1.tar.gz
  SOURCE_DIR "/users/wcd/cmake-v3/out/build/project_luajit-prefix/src/project_luajit"
  DOWNLOAD_COMMAND ""
  #PREFIX ${CMAKE_CURRENT_BINARY_DIR}/luajit-2.0.1
  CONFIGURE_COMMAND ""
  BUILD_COMMAND make
  BUILD_IN_SOURCE true
  INSTALL_COMMAND make install
)
ExternalProject_Get_Property(project_luajit SOURCE_DIR)
message("+++++" ${SOURCE_DIR})
add_executable(${PROJECT_NAME} main.c)
target_link_libraries(${PROJECT_NAME} ${SOURCE_DIR}/src/libluajit.so)

```
