Include flag (ncurses) in CMAKE project on MAC OS X [duplicate] - c++

This question already has answers here:
How to add "-l" (ell) compiler flag in CMake
(2 answers)
Closed 3 years ago.
I'm trying to familiarize myself with ncurses.
When I compile this code on my IDE (cLion), it gives me the error:
"Error opening terminal: unknown"
I'm using Mac OSX.
If I compile using the terminal with:
"g++ -lncurses main.cpp -o hello"
It compiles and runs successfully.
But I'd like to figure out how to compile and run it on cLion.
I've checked around every on this forum and haven't been able to fix the issue. I've modified the CMakeLists.txt file various ways and none worked.
Where is the issue?
main.cpp
#include <ncurses.h>
using namespace std;
int main() {
initscr();
printw("Hello");
refresh();
int c = getch();
printw("%d", c);
getch();
endwin();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(ncurses)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "-lncurses")
add_executable(lncurses main.cpp)

CMAKE_CXX_FLAGS is the variable that contains compiler flags, not linker flags or libraries.
Use the target_link_libraries command to add libraries to link with:
target_link_libraries(lncurses ncurses)

Related

CMake + ninja - exe file shows no output if header file is included in a source file

I am learning to use CMake and I'm using ninja as a build system. I have created a very simple program, but I have one issue. I have only two files main.cpp and constants.h. Whenever I include constants.h in main.cpp, after building a .exe file, I see no output from it after running it through bash. When i remove the include, it works just fine. Can anybody please guide.
main.cpp
#include <stdio.h>
#include "constants.h" // does not even show output when i add this
int main() {
printf("hello world");
return 0;
}
constants.h
#pragma once
const int WIDTH = 1000;
CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
set(CMAKE_CXX_COMIPLER "g++")
set(CMAKE_C_COMPILER "gcc")
project(BoidSimulation)
set(CMAKE_CXX_STANDARD 17)
add_executable(BoidSimulation main.cpp)
Can someone please guide what I'm doing wrong. I have tried to add constants.h in add_exectuable and also used target_include_directories by keeping the header file in some directory.
I build and run through the following commands:
cmake -G "Ninja" -S . -B ./out/build/
cd out/build
ninja
./BoidSimulation
Running this with header file shows no output and no errors on bash, it just moves on to next line. It runs fine through powershell and shows the output with or without header files.
You never print a newline. In some shells this will cause the prompt to be printed over it. This has nothing to do with your constants.h header.

How to include 'jni.h' file using CMake [duplicate]

This question already has answers here:
How to use FIND_JNI on cmake
(2 answers)
Closed 9 months ago.
I am trying to compile my C++ project using CMake on my Mac M1 Pro 12.0.1.
This is my simple directory structure:
In my foo.cpp, I am including jni.h header file:
#include <iostream>
#include "foo.h"
#include <vector>
#include <jni.h>
void foo()
{
std::cout << "Hello World!\n";
}
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.4.1)
project("myapplication")
add_library(my_app SHARED main.cpp foo.cpp)
target_include_directories(my_app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
if(APPLE)
set(JAVA_INCLUDE_PATH "$ENV{JAVA_HOME}/include")
set(JAVA_INCLUDE_PATH2 "$ENV{JAVA_HOME}/include/darwin")
set(JAVA_AWT_INCLUDE_PATH "$ENV{JAVA_HOME}/include")
message("JAVA_INCLUDE_PATH = ${JAVA_INCLUDE_PATH}")
find_package(JNI REQUIRED)
if (JNI_FOUND)
message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}")
message (STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}")
endif()
target_include_directories(my_app PUBLIC ${JNI_INCLUDE_DIRS})
endif()
I am getting CMake output as:
-- Found JNI: /System/Library/Frameworks/JavaVM.framework
-- JNI_INCLUDE_DIRS=/Users/vmangal/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/include;/Users/vmangal/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/include/darwin;/Users/vmangal/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/include
-- JNI_LIBRARIES=/System/Library/Frameworks/JavaVM.framework;/System/Library/Frameworks/JavaVM.framework
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/vmangal/my/practice/cmake/build
My JAVA_HOME environment variable is set to /Users/vmangal/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk
But during make command, I am getting compilation error:
/Users/vmangal/my/practice/cmake/foo.cpp:4:10: fatal error: 'jni.h' file not found
#include <jni.h>
Am I missing something in CMakeLists.txt ?
I had a similar problem and found that when setting variables this way
set(JAVA_INCLUDE_PATH "$ENV{JAVA_HOME}/include")
i was getting wrong imports for C_INCLUDES and CXX_INCLUDES. Like this (see in CMakeFiles/my_app.dir/flags.make):
C_INCLUDES = -I/include -I/include/darwin
CXX_INCLUDES = -I/include -I/include/darwin
Which basically which meant that $ENV{JAVA_HOME} was an empty string.
After I set the full paths in CMakeLists
set(JAVA_INCLUDE_PATH /usr/local/Cellar/openjdk#17/17.0.3/libexec/openjdk.jdk/Contents/Home/include)
everything started working correctly.
So the conclusion is that if you want to run cmake and use $ENV{JAVA_HOME} – use syntax JAVA_HOME=/path/to/java/home cmake ...

OpenMP support for Mac using clang or gcc

I am using Mac OS and I created a toy CMake project with OpenMP.
main.cpp:
#include <iostream>
#include "omp.h"
using namespace std;
int main() {
cout << "Hello, World!" << endl;
#pragma omp parallel
cout << "Hello OpenMP from thread " << omp_get_thread_num() << endl;
return 0;
}
CMakeList.txt:
cmake_minimum_required(VERSION 3.12)
project(omp_test)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
add_executable(omp_test main.cpp)
When I tried mkdir build && cd build && cmake .. && make -j to build the project, I got clang: error: unsupported option '-fopenmp'. It works well on Ubuntu. How can I change the CMakeList.txt to enable OpenMP support? Or If the problem can be solved using g++, how to do it?
Thanks for your help!
I have solved my problem by changing the default compiler to g++. (Note: I have already installed gcc/g++ and the OpenMP library from Homebrew.)
I did not change main.cpp and CMakeList.txt, but when I built the project, I use
mkdir build && cd build && cmake -DCMAKE_CXX_COMPILER=g++-9 .. && make -j
then the error clang: error: unsupported option '-fopenmp' disappeared. Specifically, I added the option -DCMAKE_CXX_COMPILER=g++-9 to change the default compiler. Maybe the g++ version on your computer is not g++-9, then you can check it under the path /user/local/bin/.

How do I link this dynamic-link library to the program? [duplicate]

This question already has answers here:
gcc/g++: "No such file or directory"
(3 answers)
Closed 3 years ago.
I have exported
/home/username/mesa/lib
LD_LIBRARY_PATH and tried to link the libraries, but I do not know what I have type wrong to compile the program.
So I tried to compile testing.cpp with the g++ command and it says :
fatal error: osmesa.h: No such file or directory
#include <osmesa.h>
I suppose I have typed wrong the command.
The command I tried: g++ testing.cpp -L/home/username/mesa/lib/libOSMesa.so -lmesa -s -Lmesa -lOSMesa -lGLU
Source code of testin.cpp:
#include <osmesa.h>
int main()
{
return 0;
}
libraries in side /home/username/mesa/lib:
libOSMesa.la libOSMesa.so libOSMesa.so.8 libOSMesa.so.8.0.0
You must pass the include directories as well, use the -I compiler option.
This is because the compiler will by default not look for headers in your home directory (it will do that for system installed libraries in /usr/include).

Adding library reference to NewT in CMake CMakeLists.txt

I am trying to make a simple TUI using newt. I have installed the newt-dev package: apt-get install libnewt-dev and I beleive it is installed correctly since if I do build using gcc with following commands, it works just fine:
gcc -o test main.cpp -lnewt
But my simple code does not compile when I try with cmake using new CLion IDE. Here are the source code, CMakeLists.txt and compiler output:
#include <newt.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
/* required variables and string */
int uiRows, uiCols;
const char pcText[] = "Welcome to Newt and FLOSS !!!";
/* initialization stuff */
newtInit();
newtCls();
/* determine current terminal window size */
uiRows = uiCols = 0;
newtGetScreenSize(&uiCols, &uiRows);
/* draw standard help and string on root window */
newtPushHelpLine(NULL);
newtDrawRootText((uiCols-strlen(pcText))/2, uiRows/2, pcText);
/* cleanup after getting a keystroke */
newtWaitForKey();
newtFinished();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(TemparatureMonitoring)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_library(newt) #this does nothing!
set(SOURCE_FILES main.cpp)
add_executable(TemparatureMonitoring ${SOURCE_FILES})
Compiler output:
/opt/clion-1.0/bin/cmake/bin/cmake --build /home/saeid/.clion10/system/cmake/generated/9c100db8/9c100db8/Debug --target all -- -j 4
You have called ADD_LIBRARY for library newt without any source files. This typically indicates a problem with your CMakeLists.txt file
-- Configuring done
CMake Error: Cannot determine link language for target "newt".
CMake Error: CMake can not determine linker language for target: newt
-- Generating done
-- Build files have been written to: /home/saeid/.clion10/system/cmake/generated/9c100db8/9c100db8/Debug
make: *** [cmake_check_build_system] Error 1
I think I have to somehow add a reference to newt package, but no idea how! So basically I am looking for a equivalent to -l switch of gcc for CMakeLists.txt
In addition to my comment, after searching a little bit, I think you need the command ''target_link_libraries''
http://www.cmake.org/cmake/help/v3.0/command/target_link_libraries.html