1

In C++, I have a library path I know how include when building with a CMakeLists.txt file but I don't know how to include it when building with a Makefile.

I tried applying the solution asked and answered here but it didn't work. The contents of the Makefile is below. The library's name is "NumCpp". The full path to this library on my computer is C:\Users\ooo\tor\Robot\rainbow\NumCpp\include\.
In the .cc file I am including the library as #include "NumCpp.hpp"

This is the CMakeLists.txt file. This would include and compile the NumCpp library if you ran this in the directory containing NumCpp. I don't know if it is helpful to show this, but I know I can include the library this way.

cmake_minimum_required(VERSION 3.14) project("HelloWorld" CXX) add_executable(${PROJECT_NAME} main.cpp) find_package(NumCpp 2.6.2 REQUIRED) target_link_libraries(${PROJECT_NAME} NumCpp::NumCpp ) 

The Makefile. I tried using INC_DIR, CFLAGS, and DEPS to link the path to the library but I'm getting an error.

COMMON=/O2 /MT /EHsc /arch:AVX /I../include /Fe../bin/ cl_vars = 'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\bin\Hostx86\x86\cl.exe' cl_var = 'cl' INC_DIR = C:/Users/ooo/tor/Robot/rainbow/NumCpp/include/ CFLAGS=-c -Wall -I$(INC_DIR) DEPS = NumCpp.hpp all: $(cl_var) $(COMMON) testxml.cc ../bin/mujoco210nogl.lib $(cl_var) $(COMMON) testspeed.cc ../bin/mujoco210nogl.lib $(cl_var) $(COMMON) compile.cc ../bin/mujoco210nogl.lib $(cl_var) $(COMMON) derivative.cc /openmp ../bin/mujoco210nogl.lib $(cl_var) $(COMMON) basic.cc ../bin/glfw3.lib ../bin/mujoco210.lib $(cl_var) $(COMMON) record.cc ../bin/glfw3.lib ../bin/mujoco210.lib $(cl_var) $(COMMON) simulate.cc ../include/uitools.c ../bin/glfw3.lib ../bin/mujoco210.lib del *.obj 

The error...

simulate.cc(14): fatal error C1083: Cannot open include file: 'NumCpp.hpp': No such file or directory Generating Code... Compiling... uitools.c Generating Code... make: *** [Makefile:16: all] Error 2 
3
  • Always use forward-slashes as directory separators in makefiles, not backslashes. Also, this makefile is pretty useless. You might as well just write a .bat batch file and put the compile lines in it, then you wouldn't need to worry about backslashes. You're just using the makefile as a glorified batch file and not taking advantage of any of the things that make makefiles useful, such as avoiding unnecessary recompilation etc. Commented Mar 22, 2022 at 18:22
  • Hm. Looking closer it doesn't seem like you're using the CFLAGS, INC_DIR, or DEPS variables at all anywhere in your makefile anyway. Just assigning those variables doesn't actually do anything, unless you reference them somewhere. I suppose you want to use the COMMON variable, to add include paths. Commented Mar 22, 2022 at 18:24
  • "use the COMMON variable" I tried forward slashes and that didn't do the trick. Do I just write COMMON and then the path? There already is a line using COMMON. When I added my path to it I got... LINK : fatal error LNK1181: cannot open input file 'C:\Users\ooo\tor\Robot\rainbow\NumCpp\include\.obj' Commented Mar 22, 2022 at 18:27

1 Answer 1

1

This line assigns a value to a makefile variable named COMMON:

COMMON=/O2 /MT /EHsc /arch:AVX /I../include /Fe../bin/ 

this line assigns a value to a makefile variable name cl_var:

cl_var = 'cl' 

These lines in the recipe use (or "expand") the variables cl_var and COMMON:

$(cl_var) $(COMMON) testxml.cc ../bin/mujoco210nogl.lib 

(etc.)

Note that nowhere in your makefile do you ever use the variables cl_vars, CFLAGS, INC_DIR, or DEPS, so those variable assignments are basically no-ops: useless. They might as well not be there at all, they have no effect on your makefile.

If you want to add a new directory to the include path, then you have to add it to the COMMON variable, because that's the variable that will be used. Since you're using Visual C++, not a POSIX compiler like GCC or Clang, the syntax is /I<path>, not -I<path>.

So, you should try something like this:

INC_DIR = C:/Users/ooo/tor/Robot/rainbow/NumCpp/include/ COMMON = /O2 /MT /EHsc /arch:AVX /I../include /I$(INC_DIR) /Fe../bin/ 

If you need more orientation, you probably need to read at least the introduction to something like the GNU make user's manual to get yourself a basic understanding of what a makefile is and how it works. SO is not equipped to provide full-fledged tutorials.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.